Debian Codegraph @

converter/pbm/pbmtoascii.c

raw
1 /* pbmtoascii.c - read a PBM image and produce ASCII graphics
2 **
3 ** Copyright (C) 1988, 1992 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include <stdbool.h>
14 #include <assert.h>
15
16 #include "mallocvar.h"
17 #include "shhopt.h"
18 #include "pbm.h"
19
20 enum Resolution {RESOLUTION_1X2, RESOLUTION_2X4};
21
22 struct CmdlineInfo {
23 /* All the information the user supplied in the command line,
24 in a form easy for the program to use.
25 */
26 const char * inputFileName; /* Filespec of input file */
27 enum Resolution resolution;
28 };
29
30
31
32 static void
33 parseCommandLine(int argc, const char ** argv,
34 struct CmdlineInfo * const cmdlineP) {
35 /*----------------------------------------------------------------------------
36 Convert program invocation arguments (argc,argv) into a format the
37 program can use easily, struct CmdlineInfo. Validate arguments along
38 the way and exit program with message if invalid.
39
40 Note that some string information we return as *cmdlineP is in the storage
41 argv[] points to.
42 -----------------------------------------------------------------------------*/
43 optEntry * option_def;
44 /* Instructions to OptParseOptions3 on how to parse our options.
45 */
46 optStruct3 opt;
47
48 unsigned int opt1x2Spec, opt2x4Spec;
49 unsigned int option_def_index;
50
51 MALLOCARRAY_NOFAIL(option_def, 100);
52
53 option_def_index = 0; /* incremented by OPTENTRY */
54 OPTENT3(0, "1x2", OPT_FLAG, NULL, &opt1x2Spec, 0);
55 OPTENT3(0, "2x4", OPT_FLAG, NULL, &opt2x4Spec, 0);
56
57 opt.opt_table = option_def;
58 opt.short_allowed = false; /* We have no short (old-fashioned) options */
59 opt.allowNegNum = false; /* We have no parms that are negative numbers */
60
61 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
62 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
63
64 free(option_def);
65
66 if (opt1x2Spec && opt2x4Spec)
67 pm_error("You cannot specify both -1x2 and -2x4");
68 else if (opt2x4Spec)
69 cmdlineP->resolution = RESOLUTION_2X4;
70 else
71 cmdlineP->resolution = RESOLUTION_1X2;
72
73 if (argc-1 < 1)
74 cmdlineP->inputFileName = "-";
75 else
76 cmdlineP->inputFileName = argv[1];
77
78 if (argc-1 > 1)
79 pm_error("Too many arguments (%u). Only possible argument is "
80 "input file name", argc-1);
81 }
82
83
84
85 /*
86 The algorithm is based on describing the 2 or 8 pixels in a cell with a
87 single integer called a signature, which we use as an index into an array to
88 get the character whose glyph best matches those pixels. The encoding is as
89 follows. Make a string of bits, with each bit being one pixel of the cell,
90 1 = black, 0 = white. The order of the string is left to right across the
91 top row, then the next row down, etc. Considering that string to be a
92 binary cipher, the integer it represents is the signature.
93
94 Example: the 2x4 cell consists of these pixels: (* = black)
95
96 * *
97 *
98
99 *
100 The bit string to represent this is 11100001. So the signature for this
101 cell is the integer 0xE1 (225).
102
103 You index the array carr2x4 with 0xE1, and get '?' as the character to
104 represent that cell. (We don't really try very hard to match the shape;
105 it's mostly important to match the density).
106 */
107
108 #define SQQ '\''
109 #define BSQ '\\'
110
111 /* Bit-map for 1x2 mode:
112 ** 1
113 ** 2
114 */
115 static char const carr1x2[4] = {
116 /* 0 1 2 3 */
117 ' ', '"', 'o', 'M' };
118
119 /* Bit-map for 2x4 mode (hex):
120 ** 1 2
121 ** 4 8
122 ** 10 20
123 ** 40 80
124 ** The idea here is first to preserve geometry, then to show density.
125 */
126 #define D08 'M'
127 #define D07 'H'
128 #define D06 '&'
129 #define D05 '$'
130 #define D04 '?'
131 static char const carr2x4[256] = {
132 /*0 1 2 3 4 5 6 7 8 9 A B C D E F */
133 ' ',SQQ, '`','"', '-',SQQ, SQQ,SQQ, '-','`', '`','`', '-','^','^','"',/*00-0F*/
134 '.',':', ':',':', '|','|', '/',D04, '/','>', '/','>', '~','+','/','*',/*10-1F*/
135 '.',':', ':',':', BSQ,BSQ, '<','<', '|',BSQ, '|',D04, '~',BSQ,'+','*',/*20-2F*/
136 '-',':', ':',':', '~',D04, '<','<', '~','>', D04,'>', '=','b','d','#',/*30-3F*/
137 '.',':', ':',':', ':','!', '/',D04, ':',':', '/',D04, ':',D04,D04,'P',/*40-4F*/
138 ',','i', '/',D04, '|','|', '|','T', '/',D04, '/','7', 'r','}','/','P',/*50-5F*/
139 ',',':', ';',D04, '>',D04, 'S','S', '/',')', '|','7', '>',D05,D05,D06,/*60-6F*/
140 'v',D04, D04,D05, '+','}', D05,'F', '/',D05, '/',D06, 'p','D',D06,D07,/*70-7F*/
141 '.',':', ':',':', ':',BSQ, ':',D04, ':',BSQ, '!',D04, ':',D04,D04,D05,/*80-8F*/
142 BSQ,BSQ, ':',D04, BSQ,'|', '(',D05, '<','%', D04,'Z', '<',D05,D05,D06,/*90-9F*/
143 ',',BSQ, 'i',D04, BSQ,BSQ, D04,BSQ, '|','|', '|','T', D04,BSQ,'4','9',/*A0-AF*/
144 'v',D04, D04,D05, BSQ,BSQ, D05,D06, '+',D05, '{',D06, 'q',D06,D06,D07,/*B0-BF*/
145 '_',':', ':',D04, ':',D04, D04,D05, ':',D04, D04,D05, ':',D05,D05,D06,/*C0-CF*/
146 BSQ,D04, D04,D05, D04,'L', D05,'[', '<','Z', '/','Z', 'c','k',D06,'R',/*D0-DF*/
147 ',',D04, D04,D05, '>',BSQ, 'S','S', D04,D05, 'J',']', '>',D06,'1','9',/*E0-EF*/
148 'o','b', 'd',D06, 'b','b', D06,'6', 'd',D06, 'd',D07, '#',D07,D07,D08 /*F0-FF*/
149 };
150
151
152
153 static void
154 makeRowOfSignatures(FILE * const ifP,
155 unsigned int const cols,
156 unsigned int const rows,
157 int const format,
158 unsigned int const cellWidth,
159 unsigned int const cellHeight,
160 unsigned int const row,
161 unsigned int * const sig,
162 unsigned int const ccols) {
163 /*----------------------------------------------------------------------------
164 Compute the signatures for every cell in a row.
165
166 Read the pixels from *ifP, which is positioned to the first pixel row
167 of the cell row, which is row number 'row'. The image dimensions are
168 'cols' x 'rows' pixels.
169
170 Each cell is 'cellWidth' x 'cellHeight'.
171
172 Return the signatures as sig[], which is 'ccols' wide because that's how
173 many cells you get from 'cols' pixels divided into cells 'cellWidth' pixels
174 wide.
175 -----------------------------------------------------------------------------*/
176 unsigned int b;
177 unsigned int subrow; /* row within cell */
178 bit * bitrow; /* malloc'ed array */
179
180 bitrow = pbm_allocrow(cols);
181 {
182 unsigned int col;
183 for (col = 0; col < ccols; ++col)
184 sig[col] = 0;
185 }
186
187 b = 0x1; /* initial value */
188 for (subrow = 0; subrow < cellHeight; ++subrow) {
189 if (row + subrow < rows) {
190 unsigned int subcol; /* col within cell */
191 pbm_readpbmrow(ifP, bitrow, cols, format);
192 for (subcol = 0; subcol < cellWidth; ++subcol) {
193 unsigned int col, ccol;
194 for (col = subcol, ccol = 0;
195 col < cols;
196 col += cellWidth, ++ccol) {
197 if (bitrow[col] == PBM_BLACK)
198 sig[ccol] |= b;
199 }
200 b <<= 1;
201 }
202 }
203 }
204 pbm_freerow(bitrow);
205 }
206
207
208
209 static void
210 findRightMargin(const unsigned int * const sig,
211 unsigned int const ccols,
212 const char * const carr,
213 unsigned int * const endColP) {
214 /*----------------------------------------------------------------------------
215 Find the first cell of the right margin, i.e. a contiguous set of
216 all-white cells at the right end of the row.
217 -----------------------------------------------------------------------------*/
218 unsigned int endcol;
219
220 for (endcol = ccols; endcol > 0; --endcol) {
221 if (carr[sig[endcol-1]] != ' ')
222 break;
223 }
224 *endColP = endcol;
225 }
226
227
228
229 static void
230 assembleCellRow(const unsigned int * const sig,
231 unsigned int const ccols,
232 const char * const carr,
233 char * const line) {
234 /*----------------------------------------------------------------------------
235 Return as line[] the line of ASCII codes for the characters of one
236 row of cells, ready for printing.
237 -----------------------------------------------------------------------------*/
238 unsigned int col;
239
240 for (col = 0; col < ccols; ++col)
241 line[col] = carr[sig[col]];
242
243 line[ccols] = '\0';
244 }
245
246
247
248 static void
249 pbmtoascii(FILE * const ifP,
250 unsigned int const cellWidth,
251 unsigned int const cellHeight,
252 const char * const carr) {
253
254 int format;
255 int cols, rows;
256 /* Dimensions of the input in pixels */
257 unsigned int ccols;
258 /* Width of the output in characters */
259 char * line; /* malloc'ed array */
260 unsigned int row;
261 unsigned int * signatureRow; /* malloc'ed array */
262 /* This is the cell signatures of a row of cells.
263 signatureRow[0] is the signature for the first (leftmost) cell
264 in the row, signatureRow[1] is the signature for the next one,
265 etc. A signature is an encoding of the pixels of a cell as
266 an integer, as described above.
267 */
268 assert(cellWidth * cellHeight <= sizeof(signatureRow[0])*8);
269
270 pbm_readpbminit(ifP, &cols, &rows, &format);
271
272 ccols = (cols + cellWidth - 1) / cellWidth;
273
274 MALLOCARRAY(signatureRow, ccols);
275 if (signatureRow == NULL)
276 pm_error("No memory for %u columns", ccols);
277
278 MALLOCARRAY_NOFAIL(line, ccols+1);
279 if (line == NULL)
280 pm_error("No memory for %u columns", ccols);
281
282 for (row = 0; row < rows; row += cellHeight) {
283 unsigned int endCol;
284
285 makeRowOfSignatures(ifP, cols, rows, format, cellWidth, cellHeight,
286 row, signatureRow, ccols);
287
288 findRightMargin(signatureRow, ccols, carr, &endCol);
289
290 assembleCellRow(signatureRow, endCol, carr, line);
291
292 puts(line);
293 }
294 free(signatureRow);
295 free(line);
296 }
297
298
299
300 int
301 main(int argc, const char ** argv) {
302
303 struct CmdlineInfo cmdline;
304 FILE * ifP;
305 unsigned int gridx, gridy;
306 const char * carr;
307
308 pm_proginit(&argc, argv);
309
310 parseCommandLine(argc, argv, &cmdline);
311
312 ifP = pm_openr(cmdline.inputFileName);
313
314 switch (cmdline.resolution) {
315 case RESOLUTION_1X2:
316 gridx = 1;
317 gridy = 2;
318 carr = carr1x2;
319 break;
320 case RESOLUTION_2X4:
321 gridx = 2;
322 gridy = 4;
323 carr = carr2x4;
324 break;
325 }
326
327 pbmtoascii(ifP, gridx, gridy, carr);
328
329 pm_close(ifP);
330
331 return 0;
332 }
333
334
335