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