Debian Codegraph @

converter/other/pamtohtmltbl.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "pm_c_util.h"
6 #include "mallocvar.h"
7 #include "shhopt.h"
8 #include "pam.h"
9
10 struct cmdlineInfo {
11 /* All the information the user supplied in the command line,
12 in a form easy for the program to use.
13 */
14 const char * inputFileName; /* '-' if stdin */
15 const char * transparent; /* NULL if none */
16 unsigned int verbose;
17 };
18
19
20
21
22 static void
23 parseCommandLine(int argc, char ** argv,
24 struct cmdlineInfo * const cmdlineP) {
25 /*----------------------------------------------------------------------------
26 parse program command line described in Unix standard form by argc
27 and argv. Return the information in the options as *cmdlineP.
28
29 If command line is internally inconsistent (invalid options, etc.),
30 issue error message to stderr and abort program.
31
32 Note that the strings we return are stored in the storage that
33 was passed to us as the argv array. We also trash *argv.
34 -----------------------------------------------------------------------------*/
35 optEntry * option_def;
36 /* Instructions to pm_optParseOptions3 on how to parse our options.
37 */
38 optStruct3 opt;
39
40 unsigned int option_def_index;
41
42 unsigned int transparentSpec;
43
44 MALLOCARRAY_NOFAIL(option_def, 100);
45
46 option_def_index = 0; /* incremented by OPTENT3 */
47 OPTENT3(0, "verbose", OPT_FLAG, NULL,
48 &cmdlineP->verbose, 0 );
49 OPTENT3(0, "transparent", OPT_STRING, &cmdlineP->transparent,
50 &transparentSpec, 0 );
51
52 opt.opt_table = option_def;
53 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
54 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
55
56 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
57 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
58
59
60 if (!transparentSpec)
61 cmdlineP->transparent = NULL;
62
63 if (argc-1 < 1)
64 cmdlineP->inputFileName = "-";
65 else if (argc-1 == 1)
66 cmdlineP->inputFileName = argv[1];
67 else
68 pm_error("Too many arguments. Program takes at most one argument: "
69 "input file name");
70 }
71
72
73
74 static void
75 pripix(struct pam * const pamP,
76 tuple const color,
77 unsigned int const rectWidth,
78 unsigned int const rectHeight,
79 tuple const transparentColor) {
80
81 if (rectWidth > 0 && rectHeight > 0) {
82 printf("<TD VALIGN=CENTER ALIGN=CENTER");
83
84 if (rectWidth != 1)
85 printf(" COLSPAN=%d", rectWidth);
86 if (rectHeight != 1)
87 printf(" ROWSPAN=%d", rectHeight);
88
89 if (transparentColor &&
90 !pnm_tupleequal(pamP, color,transparentColor)) {
91 /* No BGCOLOR attribute */
92 } else {
93 tuple const colorff = pnm_allocpamtuple(pamP);
94
95 unsigned int r, g, b;
96
97 pnm_scaletuple(pamP, colorff, color, 0xff);
98
99 if (pamP->depth < 3)
100 r = g = b = colorff[0];
101 else {
102 r = color[PAM_RED_PLANE];
103 g = color[PAM_GRN_PLANE];
104 b = color[PAM_BLU_PLANE];
105 }
106 printf(" BGCOLOR=#%02X%02X%02X", r, g, b);
107 }
108 printf(">");
109 printf("<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>"
110 "<TR><TD></TD></TR></TABLE>");
111 printf("</TD>\n");
112 }
113 }
114
115
116
117 static void
118 findSameColorRectangle(struct pam * const pamP,
119 tuple ** const tuples,
120 unsigned int const row,
121 unsigned int const col,
122 unsigned int * const rectWidthP,
123 unsigned int * const rectHeightP) {
124 /*----------------------------------------------------------------------------
125 Find the largest rectangle, in the image described by 'pam' and
126 'tuples', of uniform color, whose upper left corner is at (row, col).
127
128 Return the width and height of that rectangle as *rectWidthP
129 and *rectHeightP.
130 -----------------------------------------------------------------------------*/
131 tuple const rectangleColor = tuples[row][col];
132
133 unsigned int i;
134 unsigned int mx, my;
135 unsigned int cnx, cny;
136
137 mx=0; my=0;
138 cnx = pamP->width - col; cny = pamP->height - row;
139
140 for (i = 0; !mx || !my; ++i) {
141 int j;
142 for (j = 0; j <= i; ++j) {
143 if (!my) {
144 if (i >= cny)
145 my = cny;
146 else {
147 if ((!mx || j < mx) && (j < cnx)) {
148 if (!pnm_tupleequal(pamP, tuples[row+i][col+j],
149 rectangleColor))
150 my = i;
151 }
152 }
153 }
154 if (!mx) {
155 if (i >= cnx)
156 mx = cnx;
157 else {
158 if ((!my || (j < my)) && (j < cny)) {
159 if (!pnm_tupleequal(pamP, tuples[row+j][col+i],
160 rectangleColor))
161 mx = i;
162 }
163 }
164 }
165 }
166 }
167 *rectWidthP = mx;
168 *rectHeightP = my;
169 }
170
171
172
173 static bool **
174 allocOutputtedArray(unsigned int const width,
175 unsigned int const height) {
176
177 bool ** outputted;
178 unsigned int row;
179
180 MALLOCARRAY(outputted, height);
181 if (outputted == NULL)
182 pm_error("Unable to allocate space for 'outputted' array");
183
184 for (row = 0; row < height; ++row) {
185 MALLOCARRAY(outputted[row], width);
186 if (outputted[row] == NULL)
187 pm_error("Unable to allocate space for 'outputted' array");
188 }
189 return outputted;
190 }
191
192
193
194 static void
195 freeOutputtedArray(bool ** const outputted,
196 unsigned int const height) {
197
198 unsigned int row;
199
200 for (row = 0; row < height; ++row)
201 free(outputted[row]);
202 }
203
204
205
206 static void
207 markNotOutputted(bool ** const outputted,
208 unsigned int const upperLeftCol,
209 unsigned int const upperLeftRow,
210 unsigned int const width,
211 unsigned int const height) {
212 /*----------------------------------------------------------------------------
213 Mark every pixel in the specified rectangle as not having been output
214 yet.
215 -----------------------------------------------------------------------------*/
216 unsigned int const lowerRightCol = upperLeftCol + width;
217 unsigned int const lowerRightRow = upperLeftRow + height;
218 unsigned int row;
219
220 for (row = upperLeftRow; row < lowerRightRow; ++row) {
221 unsigned int col;
222 for (col = upperLeftCol; col < lowerRightCol; ++col)
223 outputted[row][col] = FALSE;
224 }
225 }
226
227
228
229 static void
230 markOutputted(bool ** const outputted,
231 unsigned int const upperLeftCol,
232 unsigned int const upperLeftRow,
233 unsigned int const width,
234 unsigned int const height) {
235 /*----------------------------------------------------------------------------
236 Mark every pixel in the specified rectangle as having been output
237 already.
238 -----------------------------------------------------------------------------*/
239 unsigned int const lowerRightCol = upperLeftCol + width;
240 unsigned int const lowerRightRow = upperLeftRow + height;
241 unsigned int row;
242
243 for (row = upperLeftRow; row < lowerRightRow; ++row) {
244 unsigned int col;
245 for (col = upperLeftCol; col < lowerRightCol; ++col)
246 outputted[row][col] = TRUE;
247 }
248 }
249
250
251
252 int
253 main(int argc, char **argv) {
254 struct cmdlineInfo cmdline;
255 FILE * ifP;
256 struct pam inpam;
257 tuple ** tuples;
258 unsigned int row;
259 unsigned int rectWidth, rectHeight;
260 bool ** outputted;
261 /* Two dimensional array. outputted[ROW][COL] means the pixel
262 at row ROW, column COL has already been outputted.
263 */
264 tuple transparentColor;
265
266 pnm_init(&argc, argv);
267
268 parseCommandLine(argc, argv, &cmdline);
269
270 ifP = pm_openr(cmdline.inputFileName);
271
272 tuples = pnm_readpam(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
273
274 if (cmdline.transparent) {
275 pixel transcolor;
276 transcolor = ppm_parsecolor(cmdline.transparent, inpam.maxval);
277 transparentColor = pnm_allocpamtuple(&inpam);
278 transparentColor[PAM_RED_PLANE] = PPM_GETR(transcolor);
279 transparentColor[PAM_GRN_PLANE] = PPM_GETG(transcolor);
280 transparentColor[PAM_BLU_PLANE] = PPM_GETB(transcolor);
281 } else
282 transparentColor = NULL;
283
284 outputted = allocOutputtedArray(inpam.width, inpam.height);
285
286 printf("<TABLE WIDTH=%d HEIGHT=%d BORDER=0 CELLSPACING=0 CELLPADDING=0>\n",
287 inpam.width, inpam.height);
288
289 markNotOutputted(outputted, 0, 0, inpam.width, inpam.height);
290 /* No pixel has been outputted yet */
291
292 for (row = 0; row < inpam.height; ++row) {
293 unsigned int col;
294 printf("<TR>\n");
295 pripix(&inpam, tuples[row][0], 1, 1, transparentColor);
296 markOutputted(outputted, 0, row, 1, 1);
297
298 for (col = 1; col < inpam.width; ++col) {
299 if (!outputted[row][col]) {
300 findSameColorRectangle(&inpam, tuples, row, col,
301 &rectWidth, &rectHeight);
302 if (cmdline.verbose)
303 pm_message("[%u/%u] [%u/%u]",
304 col, row, rectWidth, rectHeight);
305 pripix(&inpam, tuples[row][col], rectWidth, rectHeight,
306 transparentColor);
307 markOutputted(outputted, col, row, rectWidth, rectHeight);
308 }
309 }
310 printf("</TR>\n");
311 }
312 printf("</TABLE>\n");
313
314 if (transparentColor)
315 pnm_freepamtuple(transparentColor);
316 pnm_freepamarray(tuples, &inpam);
317 freeOutputtedArray(outputted, inpam.height);
318
319 return 0;
320 }
321
322
323