Debian Codegraph @

converter/pbm/pbmto4425.c

raw
1 #include <assert.h>
2 #include <string.h>
3
4 #include "nstring.h"
5 #include "mallocvar.h"
6 #include "pbm.h"
7
8 static char const bit_table[2][3] = {
9 {1, 4, 0x10},
10 {2, 8, 0x40}
11 };
12
13 static unsigned int const vmapWidth = 132;
14 static unsigned int const vmapHeight = 23;
15
16
17
18 static void
19 initMap(char * const vmap) {
20
21 unsigned int col;
22
23 for (col = 0; col < vmapWidth; ++col) {
24 unsigned int row;
25
26 for (row = 0; row < vmapHeight; ++row)
27 vmap[row * vmapWidth + col] = ' ';
28 }
29 }
30
31
32
33 static void
34 setVmap(char * const vmap,
35 unsigned int const x,
36 unsigned int const y) {
37
38 unsigned int const ix = x/2;
39 unsigned int const iy = y/3;
40
41 assert(ix < vmapWidth);
42 assert(iy < vmapHeight);
43
44 vmap[iy * vmapWidth + ix] |= bit_table[x % 2][y % 3];
45 }
46
47
48
49 static void
50 fillMap(FILE * const pbmFileP,
51 char * const vmap) {
52
53 unsigned int const xres = vmapWidth * 2;
54 unsigned int const yres = vmapHeight * 3;
55
56 bit ** pbmImage;
57 int cols;
58 int rows;
59 unsigned int row;
60
61 pbmImage = pbm_readpbm(pbmFileP, &cols, &rows);
62
63 for (row = 0; row < rows && row < yres; ++row) {
64 unsigned int col;
65
66 for (col = 0; col < cols && col < xres; ++col) {
67 if (pbmImage[row][col] == PBM_WHITE)
68 setVmap(vmap, col, row);
69 }
70 }
71 }
72
73
74
75 static void
76 printMap(char * const vmap,
77 FILE * const ofP) {
78
79 unsigned int row;
80
81 fputs("\033[H\033[J", ofP); /* clear screen */
82 fputs("\033[?3h", ofP); /* 132 column mode */
83 fputs("\033)}\016", ofP); /* mosaic mode */
84
85 for (row = 0; row < vmapHeight; ++row) {
86 unsigned int endCol;
87 /* Column number just past the non-space data in the row;
88 (i.e. spaces on the right are padding; not data
89 */
90 unsigned int col;
91
92 for (endCol = vmapWidth;
93 endCol > 0 && vmap[row * vmapWidth + (endCol-1)] == ' ';
94 --endCol)
95 ;
96
97 for (col = 0; col < endCol; ++col)
98 fputc(vmap[row * vmapWidth + col], ofP);
99
100 fputc('\n', ofP);
101 }
102
103 fputs("\033(B\017", ofP);
104 }
105
106
107
108 int
109 main(int argc, const char ** argv) {
110
111 unsigned int argn;
112 const char * inputFileNm;
113 FILE * ifP;
114
115 char * vmap; /* malloced */
116
117 pm_proginit(&argc, argv);
118
119 for (argn = 1;
120 argn < argc && argv[argn][0] == '-' && strlen(argv[argn]) > 1;
121 ++argn) {
122 pm_error("Unrecognized option '%s'", argv[argn]);
123 }
124
125 if (argn >= argc) {
126 inputFileNm = "-";
127 } else if(argc - argn != 1) {
128 pm_error("Too many arguments. At most one argument is allowed: "
129 "Name of the input file");
130 } else {
131 inputFileNm = argv[argn];
132 }
133
134 ifP = pm_openr(inputFileNm);
135
136 assert(vmapWidth < UINT_MAX/vmapHeight);
137
138 MALLOCARRAY(vmap, vmapWidth * vmapHeight);
139 if (!vmap)
140 pm_error("Cannot allocate memory for %u x %u pixels",
141 vmapWidth, vmapHeight);
142
143 initMap(vmap);
144 fillMap(ifP, vmap);
145 printMap(vmap, stdout);
146
147 free(vmap);
148
149 /* If the program failed, it previously aborted with nonzero completion
150 code, via various function calls.
151 */
152 return 0;
153 }
154
155
156