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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.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;
unsigned int verbose;
};
static void
parseCommandLine(int argc, const char ** const argv,
struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
Note that the file spec array we return is stored in the storage that
was passed to us as the argv array.
-----------------------------------------------------------------------------*/
optEntry *option_def;
/* Instructions to OptParseOptions3 on how to parse our options.
*/
optStruct3 opt;
unsigned int option_def_index;
MALLOCARRAY_NOFAIL(option_def, 100);
option_def_index = 0; /* incremented by OPTENTRY */
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 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 */
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
/* Uses and sets argc, argv, and some of *cmdlineP and others. */
if (argc-1 < 1)
cmdlineP->inputFileName = "-";
else {
cmdlineP->inputFileName = argv[1];
if (argc-1 > 1)
pm_error("There is at most one argument: input file name. "
"You specified %d", argc-1);
}
}
static void
initOutpam(const struct pam * const inpamP,
struct pam * const outpamP) {
outpamP->file = stdout;
outpamP->format = PAM_FORMAT;
outpamP->plainformat = 0;
outpamP->width = inpamP->width;
outpamP->height = inpamP->height;
outpamP->depth = 1;
outpamP->maxval = 1;
outpamP->bytes_per_sample = pnm_bytespersample(outpamP->maxval);
outpamP->len = PAM_STRUCT_SIZE(bytes_per_sample);
outpamP->size = sizeof(*outpamP);
}
static void
allocateOutputPointerRow(unsigned int const width,
tuple ** const tuplerowP) {
MALLOCARRAY(*tuplerowP, width);
if (*tuplerowP == NULL)
pm_error("Could not allocate a %u-column tuple pointer array", width);
}
static void
createWhiteTuple(const struct pam * const pamP,
tuple * const whiteTupleP) {
/*----------------------------------------------------------------------------
Create a "white" tuple. By that we mean a tuple all of whose elements
are zero. If it's an RGB, grayscale, or b&w pixel, that means it's black.
-----------------------------------------------------------------------------*/
tuple whiteTuple;
unsigned int plane;
whiteTuple = pnm_allocpamtuple(pamP);
for (plane = 0; plane < pamP->depth; ++plane)
whiteTuple[plane] = pamP->maxval;
*whiteTupleP = whiteTuple;
}
static void
selectBackground(struct pam * const pamP,
tuple const ul,
tuple const ur,
tuple const lr,
tuple const ll,
tuple * const bgColorP) {
tuple bg; /* Reference to one of ul, ur, ll, lr */
if (pnm_tupleequal(pamP, ul, ur) &&
(pnm_tupleequal(pamP, ul, ll) ||
pnm_tupleequal(pamP, ul, lr)))
bg = ul;
else if (pnm_tupleequal(pamP, ll, lr) &&
pnm_tupleequal(pamP, lr, ul))
bg = ll;
else {
/* No 3 corners are same color; look for 2 corners */
if (pnm_tupleequal(pamP, ul, ur)) /* top edge */
bg = ul;
else if (pnm_tupleequal(pamP, ul, ll)) /* left edge */
bg = ul;
else if (pnm_tupleequal(pamP, ur, lr)) /* right edge */
bg = ur;
else if (pnm_tupleequal(pamP, ll, lr)) /* bottom edge */
bg = ll;
else {
/* No two corners are same color; just use upper left corner */
bg = ul;
}
}
*bgColorP = pnm_allocpamtuple(pamP);
pnm_assigntuple(pamP, *bgColorP, bg);
}
static void
determineBackgroundColor(struct pam * const pamP,
bool const verbose,
tuple * const bgColorP) {
/*----------------------------------------------------------------------------
Determine what color is the background color of the image in the
file represented by *pamP.
Expect the file to be positioned to the start of the raster, and leave
it positioned arbitrarily.
-----------------------------------------------------------------------------*/
unsigned int row;
tuple * tuplerow;
tuple ul, ur, ll, lr;
/* Color of upper left, upper right, lower left, lower right */
tuplerow = pnm_allocpamrow(pamP);
ul = pnm_allocpamtuple(pamP);
ur = pnm_allocpamtuple(pamP);
ll = pnm_allocpamtuple(pamP);
lr = pnm_allocpamtuple(pamP);
pnm_readpamrow(pamP, tuplerow);
pnm_assigntuple(pamP, ul, tuplerow[0]);
pnm_assigntuple(pamP, ur, tuplerow[pamP->width-1]);
for (row = 1; row < pamP->height; ++row)
pnm_readpamrow(pamP, tuplerow);
pnm_assigntuple(pamP, ll, tuplerow[0]);
pnm_assigntuple(pamP, lr, tuplerow[pamP->width-1]);
selectBackground(pamP, ul, ur, ll, lr, bgColorP);
if (verbose) {
int const hexokTrue = 1;
const char * const colorname =
pnm_colorname(pamP, *bgColorP, hexokTrue);
pm_message("Background color is %s", colorname);
pm_strfree(colorname);
}
pnm_freepamtuple(lr);
pnm_freepamtuple(ll);
pnm_freepamtuple(ur);
pnm_freepamtuple(ul);
pnm_freepamrow(tuplerow);
}
static unsigned char const PT_UNKNOWN = 0;
static unsigned char const PT_BG = 1;
static unsigned char const PT_FG = 2;
static unsigned char **
newPi(unsigned int const width,
unsigned int const height) {
unsigned char ** pi;
unsigned int row;
MALLOCARRAY(pi, height);
if (pi == NULL)
pm_error("Out of memory allocating %u row pointers", height);
for (row = 0; row < height; ++row) {
MALLOCARRAY(pi[row], width);
if (pi[row] == NULL)
pm_error("Out of memory allocating row %u", row);
}
return pi;
}
static void
destroyPi(const unsigned char *const * const pi,
unsigned int const height) {
unsigned int row;
for (row = 0; row < height; ++row)
free((void*)pi[row]);
free((void*)pi);
}
static void
initPi(unsigned char ** const pi,
const struct pam * const pamP,
tuple const backgroundColor) {
/*----------------------------------------------------------------------------
Set the initial info about every pixel in pi[][].
Read through the image in the file described by *inpamP and set each
pixel which is not of background color, and therefore obviously
foreground, to type PT_FG. Set every other pixel to PT_UNKNOWN.
-----------------------------------------------------------------------------*/
tuple * tuplerow;
unsigned int row;
tuplerow = pnm_allocpamrow(pamP);
for (row = 0; row < pamP->height; ++row) {
unsigned int col;
pnm_readpamrow(pamP, tuplerow);
for (col = 0; col < pamP->width; ++col) {
pi[row][col] =
pnm_tupleequal(pamP, tuplerow[col], backgroundColor) ?
PT_UNKNOWN : PT_FG;
}
}
pnm_freepamrow(tuplerow);
}
static void
setEdges(unsigned char ** const pi,
unsigned int const width,
unsigned int const height) {
/*----------------------------------------------------------------------------
Do the four edges.
Anything of background color in an edge is background. Pixel type
PT_UNKNOWN implies background color, so we change all PT_UNKNOWN pixels
to PT_BG.
-----------------------------------------------------------------------------*/
unsigned int row, col;
for (col = 0; col < width; ++col) {
if (pi[0][col] == PT_UNKNOWN)
pi[0][col] = PT_BG;
if (pi[height-1][col] == PT_UNKNOWN)
pi[height-1][col] = PT_BG;
}
for (row = 0; row < height; ++row) {
if (pi[row][0] == PT_UNKNOWN)
pi[row][0] = PT_BG;
if (pi[row][width-1] == PT_UNKNOWN)
pi[row][width-1] = PT_BG;
}
}
static void
expandBackgroundHoriz(unsigned char ** const pi,
unsigned int const width,
unsigned int const height,
bool * const expandedP) {
/*----------------------------------------------------------------------------
In every row except top and bottom, expand the background rightward from
any known background pixel through all consecutive unknown pixels.
Then do the same thing leftward.
Iff we managed to expand the background at all, return *expandedP == TRUE.
-----------------------------------------------------------------------------*/
unsigned int row;
bool expanded;
for (row = 1, expanded = FALSE; row < height-1; ++row) {
int col;
for (col = 1; col < width - 1; ++col) {
if (pi[row][col] == PT_UNKNOWN && pi[row][col-1] == PT_BG) {
expanded = TRUE;
/* Set the whole consecutive row of unknown to background */
for (; pi[row][col] == PT_UNKNOWN && col < width - 1; ++col)
pi[row][col] = PT_BG;
}
}
for (col = width-2; col > 0; --col) {
if (pi[row][col] == PT_UNKNOWN && pi[row][col+1] == PT_BG) {
expanded = TRUE;
/* Set the whole consecutive row of unknown to background */
for (; pi[row][col] == PT_UNKNOWN && col > 0; --col)
pi[row][col] = PT_BG;
}
}
}
*expandedP = expanded;
}
static void
expandBackgroundVert(unsigned char ** const pi,
unsigned int const width,
unsigned int const height,
bool * const expandedP) {
/*----------------------------------------------------------------------------
In every column except leftmost and rightmost, expand the background
downward from any known background pixel through all consecutive unknown
pixels.
Then do the same thing upward.
Iff we managed to expand the background at all, return *expandedP == TRUE.
-----------------------------------------------------------------------------*/
unsigned int col;
bool expanded;
for (col = 1, expanded = FALSE; col < width-1; ++col) {
int row;
for (row = 1; row < height - 1; ++row) {
if (pi[row][col] == PT_UNKNOWN && pi[row-1][col] == PT_BG) {
expanded = TRUE;
/* Set the whole consecutive col of unknown to background */
for (; pi[row][col] == PT_UNKNOWN && row < height - 1; ++row)
pi[row][col] = PT_BG;
}
}
for (row = height-2; row > 0; --row) {
if (pi[row][col] == PT_UNKNOWN && pi[row+1][col] == PT_BG) {
expanded = TRUE;
/* Set the whole consecutive col of unknown to background */
for (; pi[row][col] == PT_UNKNOWN && row > 0; --row)
pi[row][col] = PT_BG;
}
}
}
*expandedP = expanded;
}
static void
findBackgroundPixels(struct pam * const inpamP,
tuple const backgroundColor,
bool const verbose,
const unsigned char * const ** const piP) {
/*----------------------------------------------------------------------------
Figure out which pixels of the image are background. Read the
image from the input file described by *inpamP (positioned now to
the start of the raster) and generate the matrix *piP telling which
pixels are background and which are foreground, given that the
background color is 'backgroundColor'.
Note that it isn't as simple as finding which pixels are of color
'backgroundColor'. They have to be part of a contiguous region that
touches one of the 4 edges of the image.
In the matrix we return, there is one element for each pixel in the
image, and it has value PT_BG or PT_FG.
-----------------------------------------------------------------------------*/
unsigned char ** pi;
bool backgroundComplete;
unsigned int passCt;
pi = newPi(inpamP->width, inpamP->height);
initPi(pi, inpamP, backgroundColor);
setEdges(pi, inpamP->width, inpamP->height);
backgroundComplete = FALSE;
passCt = 0;
while (!backgroundComplete) {
bool expandedHoriz, expandedVert;
expandBackgroundHoriz(pi, inpamP->width, inpamP->height,
&expandedHoriz);
expandBackgroundVert(pi, inpamP->width, inpamP->height,
&expandedVert);
backgroundComplete = !expandedHoriz && !expandedVert;
++passCt;
}
if (verbose)
pm_message("Background found in %u passes", passCt);
*piP = (const unsigned char * const *)pi;
}
static void
writeOutput(const struct pam * const inpamP,
const unsigned char * const * const pi) {
tuple black, white;
tuple * outputTuplerow;
/* This is not a normal tuplerow; it is just pointers to either
'black' or 'white'
*/
unsigned int row;
struct pam outpam;
initOutpam(inpamP, &outpam);
allocateOutputPointerRow(outpam.width, &outputTuplerow);
pnm_createBlackTuple(&outpam, &black);
createWhiteTuple(&outpam, &white);
pnm_writepaminit(&outpam);
for (row = 0; row < outpam.height; ++row) {
unsigned int col;
for (col = 0; col < outpam.width; ++col)
outputTuplerow[col] = pi[row][col] == PT_BG ? white : black;
pnm_writepamrow(&outpam, outputTuplerow);
}
pnm_freepamtuple(white);
pnm_freepamtuple(black);
free(outputTuplerow);
}
int
main(int argc, const char *argv[]) {
struct CmdlineInfo cmdline;
struct pam inpam;
FILE * ifP;
pm_filepos rasterpos;
tuple backgroundColor;
const unsigned char * const * pi;
pm_proginit(&argc, argv);
parseCommandLine(argc, argv, &cmdline);
ifP = pm_openr_seekable(cmdline.inputFileName);
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
pm_tell2(ifP, &rasterpos, sizeof(rasterpos));
determineBackgroundColor(&inpam, cmdline.verbose, &backgroundColor);
pm_seek2(ifP, &rasterpos, sizeof(rasterpos));
findBackgroundPixels(&inpam, backgroundColor, cmdline.verbose, &pi);
writeOutput(&inpam, pi);
destroyPi(pi, inpam.height);
pm_close(ifP);
pnm_freepamtuple(backgroundColor);
return 0;
}
|