| 1 |
/*============================================================================ |
| 2 |
pnmindex |
| 3 |
============================================================================== |
| 4 |
|
| 5 |
Build a visual index of a bunch of PNM images. |
| 6 |
|
| 7 |
This used to be a C shell program, and then a BASH program. Neither |
| 8 |
were portable enough, and the program is too complex for either of |
| 9 |
those languages anyway, so now it's in C. |
| 10 |
|
| 11 |
By Bryan Henderson 2005.04.24. |
| 12 |
|
| 13 |
Contributed to the public domain by its author. |
| 14 |
|
| 15 |
============================================================================*/ |
| 16 |
|
| 17 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 18 |
#define _C99_SOURCE /* Make sure snprintf() is in stdio.h */ |
| 19 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 20 |
#define _BSD_SOURCE /* Make sure strdup is in string.h */ |
| 21 |
|
| 22 |
#include <assert.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <stdarg.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
|
| 29 |
|
| 30 |
#include "pm_config.h" |
| 31 |
#include "pm_c_util.h" |
| 32 |
#include "shhopt.h" |
| 33 |
#include "mallocvar.h" |
| 34 |
#include "nstring.h" |
| 35 |
#include "pm_system.h" |
| 36 |
#include "pnm.h" |
| 37 |
|
| 38 |
struct CmdlineInfo { |
| 39 |
/* All the information the user supplied in the command line, |
| 40 |
in a form easy for the program to use. |
| 41 |
*/ |
| 42 |
unsigned int inputFileCount; |
| 43 |
const char ** inputFileName; |
| 44 |
unsigned int size; |
| 45 |
unsigned int across; |
| 46 |
unsigned int colors; |
| 47 |
unsigned int black; |
| 48 |
unsigned int noquant; |
| 49 |
const char * title; |
| 50 |
unsigned int verbose; |
| 51 |
}; |
| 52 |
|
| 53 |
static bool verbose; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static const char * |
| 58 |
shellQuote(const char * const arg) { |
| 59 |
/*---------------------------------------------------------------------------- |
| 60 |
A string that in a Bourne shell command forms a single token whose value |
| 61 |
understood by the shell is 'arg'. |
| 62 |
|
| 63 |
For example, |
| 64 |
|
| 65 |
'arg' result |
| 66 |
-------------- -------------- |
| 67 |
|
| 68 |
hello 'hello' |
| 69 |
two words 'two words' |
| 70 |
'Bryan's' \''Bryan'\''s'\' |
| 71 |
|
| 72 |
Note that in the last example the result is a concatenation of two |
| 73 |
single-quoted strings and 3 esaped single quotes outside of those quoted |
| 74 |
strings. |
| 75 |
|
| 76 |
You can use this safely to insert an arbitrary string from an untrusted |
| 77 |
source into a shell command without worrying about it changing the nature |
| 78 |
of the shell command. |
| 79 |
|
| 80 |
In newly malloced storage. |
| 81 |
-----------------------------------------------------------------------------*/ |
| 82 |
unsigned int const worstCaseResultLen = 3 * strlen(arg) + 1; |
| 83 |
|
| 84 |
char * buffer; |
| 85 |
unsigned int i; |
| 86 |
unsigned int cursor; |
| 87 |
bool inquotes; |
| 88 |
|
| 89 |
MALLOCARRAY_NOFAIL(buffer, worstCaseResultLen); |
| 90 |
|
| 91 |
for (i = 0, cursor=0, inquotes=false; i < strlen(arg); ++i) { |
| 92 |
if (arg[i] == '\'') { |
| 93 |
if (inquotes) { |
| 94 |
buffer[cursor++] = '\''; /* Close the quotation */ |
| 95 |
inquotes = false; |
| 96 |
} |
| 97 |
assert(!inquotes); |
| 98 |
buffer[cursor++] = '\\'; /* Add escaped */ |
| 99 |
buffer[cursor++] = '\''; /* single quote */ |
| 100 |
} else { |
| 101 |
if (!inquotes) { |
| 102 |
buffer[cursor++] = '\''; |
| 103 |
inquotes = true; |
| 104 |
} |
| 105 |
assert(inquotes); |
| 106 |
buffer[cursor++] = arg[i]; |
| 107 |
} |
| 108 |
} |
| 109 |
if (inquotes) |
| 110 |
buffer[cursor++] = '\''; /* Close the final quotation */ |
| 111 |
|
| 112 |
buffer[cursor++] = '\0'; /* Terminate string */ |
| 113 |
|
| 114 |
assert(cursor <= worstCaseResultLen); |
| 115 |
|
| 116 |
return buffer; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
static void |
| 123 |
parseCommandLine(int argc, const char ** argv, |
| 124 |
struct CmdlineInfo * const cmdlineP) { |
| 125 |
|
| 126 |
unsigned int option_def_index; |
| 127 |
optEntry *option_def; |
| 128 |
/* Instructions to pm_optParseOptions3 on how to parse our options. |
| 129 |
*/ |
| 130 |
optStruct3 opt; |
| 131 |
|
| 132 |
unsigned int quant; |
| 133 |
unsigned int sizeSpec, colorsSpec, acrossSpec, titleSpec; |
| 134 |
|
| 135 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 136 |
|
| 137 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 138 |
OPTENT3(0, "black", OPT_FLAG, NULL, |
| 139 |
&cmdlineP->black, 0); |
| 140 |
OPTENT3(0, "noquant", OPT_FLAG, NULL, |
| 141 |
&cmdlineP->noquant, 0); |
| 142 |
OPTENT3(0, "quant", OPT_FLAG, NULL, |
| 143 |
&quant, 0); |
| 144 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 145 |
&cmdlineP->verbose, 0); |
| 146 |
OPTENT3(0, "size", OPT_UINT, &cmdlineP->size, |
| 147 |
&sizeSpec, 0); |
| 148 |
OPTENT3(0, "colors", OPT_UINT, &cmdlineP->colors, |
| 149 |
&colorsSpec, 0); |
| 150 |
OPTENT3(0, "across", OPT_UINT, &cmdlineP->across, |
| 151 |
&acrossSpec, 0); |
| 152 |
OPTENT3(0, "title", OPT_STRING, &cmdlineP->title, |
| 153 |
&titleSpec, 0); |
| 154 |
|
| 155 |
opt.opt_table = option_def; |
| 156 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 157 |
opt.allowNegNum = FALSE; |
| 158 |
|
| 159 |
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); |
| 160 |
/* Uses and sets argc, argv, and some of *cmdline_p and others. */ |
| 161 |
|
| 162 |
if (quant && cmdlineP->noquant) |
| 163 |
pm_error("You can't specify both -quant and -noquant"); |
| 164 |
|
| 165 |
if (colorsSpec) { |
| 166 |
if (cmdlineP->colors == 0) |
| 167 |
pm_error("-colors value must be positive"); |
| 168 |
} else |
| 169 |
cmdlineP->colors = 256; |
| 170 |
|
| 171 |
if (sizeSpec) { |
| 172 |
if (cmdlineP->size == 0) |
| 173 |
pm_error("-size value must be positive"); |
| 174 |
} else |
| 175 |
cmdlineP->size = 100; |
| 176 |
|
| 177 |
if (acrossSpec) { |
| 178 |
if (cmdlineP->across == 0) |
| 179 |
pm_error("-across value must be positive"); |
| 180 |
} else |
| 181 |
cmdlineP->across = 6; |
| 182 |
|
| 183 |
if (!titleSpec) |
| 184 |
cmdlineP->title = NULL; |
| 185 |
|
| 186 |
if (colorsSpec && cmdlineP->noquant) |
| 187 |
pm_error("-colors doesn't make any sense with -noquant"); |
| 188 |
|
| 189 |
if (argc-1 < 1) |
| 190 |
pm_error("You must name at least one file that contains an image " |
| 191 |
"to go into the index"); |
| 192 |
|
| 193 |
cmdlineP->inputFileCount = argc-1; |
| 194 |
|
| 195 |
MALLOCARRAY_NOFAIL(cmdlineP->inputFileName, cmdlineP->inputFileCount); |
| 196 |
|
| 197 |
{ |
| 198 |
unsigned int i; |
| 199 |
for (i = 0; i < cmdlineP->inputFileCount; ++i) { |
| 200 |
cmdlineP->inputFileName[i] = strdup(argv[i+1]); |
| 201 |
if (cmdlineP->inputFileName[i] == NULL) |
| 202 |
pm_error("Unable to allocate memory for a file name"); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static void |
| 210 |
freeCmdline(struct CmdlineInfo const cmdline) { |
| 211 |
|
| 212 |
unsigned int i; |
| 213 |
|
| 214 |
for (i = 0; i < cmdline.inputFileCount; ++i) |
| 215 |
pm_strfree(cmdline.inputFileName[i]); |
| 216 |
|
| 217 |
free(cmdline.inputFileName); |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static void |
| 224 |
validateTmpdir(const char * const dirNm) { |
| 225 |
/*---------------------------------------------------------------------------- |
| 226 |
Abort program if 'dirNm' contains special characters that would cause |
| 227 |
trouble if included in a shell command. |
| 228 |
|
| 229 |
The cleanest thing to do would be to allow any characters in the directory |
| 230 |
name and just quote and escape the properly every time we include the name |
| 231 |
of a temporary file in a shell command, but we're too lazy for that. |
| 232 |
-----------------------------------------------------------------------------*/ |
| 233 |
if ( |
| 234 |
strchr(dirNm, '\\') || |
| 235 |
strchr(dirNm, '\'') || |
| 236 |
strchr(dirNm, ' ') || |
| 237 |
strchr(dirNm, '"') || |
| 238 |
strchr(dirNm, '$') || |
| 239 |
strchr(dirNm, '`') || |
| 240 |
strchr(dirNm, '!') |
| 241 |
) { |
| 242 |
pm_error("TMPDIR environment variable contains a shell special " |
| 243 |
"character; this program cannot use that."); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
static void |
| 250 |
makeTempDir(const char ** const tempDirP) { |
| 251 |
|
| 252 |
const char * const tmpdir = getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"; |
| 253 |
|
| 254 |
validateTmpdir(tmpdir); |
| 255 |
|
| 256 |
const char * mytmpdir; |
| 257 |
int rc; |
| 258 |
|
| 259 |
pm_asprintf(&mytmpdir, "%s/pnmindex_%d", tmpdir, getpid()); |
| 260 |
|
| 261 |
rc = pm_mkdir(mytmpdir, 0700); |
| 262 |
if (rc != 0) |
| 263 |
pm_error("Unable to create temporary file directory '%s'. mkdir() " |
| 264 |
"fails with errno %d (%s)", |
| 265 |
mytmpdir, errno, strerror(errno)); |
| 266 |
|
| 267 |
*tempDirP = mytmpdir; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
static void |
| 273 |
removeTempDir(const char * const tempDir) { |
| 274 |
|
| 275 |
int rc; |
| 276 |
|
| 277 |
rc = rmdir(tempDir); |
| 278 |
if (rc != 0) |
| 279 |
pm_error("Failed to remove temporary file directory '%s'. " |
| 280 |
"rmdir() fails with errno %d (%s)", |
| 281 |
tempDir, errno, strerror(errno)); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
static const char * |
| 286 |
rowFileName(const char * const dirName, |
| 287 |
unsigned int const row) { |
| 288 |
|
| 289 |
const char * fileName; |
| 290 |
|
| 291 |
pm_asprintf(&fileName, "%s/pi.%u", dirName, row); |
| 292 |
|
| 293 |
return fileName; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
static void |
| 299 |
makeTitle(const char * const title, |
| 300 |
unsigned int const rowNumber, |
| 301 |
bool const blackBackground, |
| 302 |
const char * const dirNm) { |
| 303 |
/*---------------------------------------------------------------------------- |
| 304 |
Create a PBM file containing the text 'title'. |
| 305 |
|
| 306 |
If 'blackBackground' is true, make it white on black; otherwise, black |
| 307 |
on white. |
| 308 |
|
| 309 |
Name the file like a thumbnail row file for row number 'rowNumber', |
| 310 |
in directory named 'dirNm'. |
| 311 |
-----------------------------------------------------------------------------*/ |
| 312 |
const char * const invertStage = blackBackground ? "| pnminvert " : ""; |
| 313 |
|
| 314 |
const char * fileName; |
| 315 |
FILE * outFP; |
| 316 |
struct bufferDesc titleBuf; |
| 317 |
const char * shellCommand; |
| 318 |
int termStatus; |
| 319 |
|
| 320 |
titleBuf.size = strlen(title); |
| 321 |
titleBuf.buffer = (unsigned char *)title; |
| 322 |
titleBuf.bytesTransferredP = NULL; |
| 323 |
|
| 324 |
fileName = rowFileName(dirNm, rowNumber); |
| 325 |
outFP = pm_openw(fileName); |
| 326 |
pm_strfree(fileName); |
| 327 |
|
| 328 |
pm_asprintf(&shellCommand, "pbmtext %s", invertStage); |
| 329 |
|
| 330 |
pm_system2(&pm_feed_from_memory, &titleBuf, |
| 331 |
&pm_accept_to_filestream, outFP, |
| 332 |
shellCommand, &termStatus); |
| 333 |
|
| 334 |
pm_strfree(shellCommand); |
| 335 |
|
| 336 |
if (termStatus != 0) |
| 337 |
pm_error("Failed to generate title image"); |
| 338 |
|
| 339 |
pm_close(outFP); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
static void |
| 345 |
copyImage(const char * const inputFileNm, |
| 346 |
const char * const outputFileNm) { |
| 347 |
|
| 348 |
int termStatus; |
| 349 |
|
| 350 |
pm_system2_lp("cat", |
| 351 |
&pm_feed_from_file, (void*)inputFileNm, |
| 352 |
&pm_accept_to_file, (void*)outputFileNm, |
| 353 |
&termStatus, |
| 354 |
"cat", NULL); |
| 355 |
|
| 356 |
if (termStatus != 0) |
| 357 |
pm_error("'cat' to copy image '%s' to '%s' failed, " |
| 358 |
"termination status = %d", |
| 359 |
inputFileNm, outputFileNm, termStatus); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
static void |
| 365 |
copyScaleQuantImage(const char * const inputFileNm, |
| 366 |
const char * const outputFileNm, |
| 367 |
int const format, |
| 368 |
unsigned int const size, |
| 369 |
unsigned int const quant, |
| 370 |
unsigned int const colorCt) { |
| 371 |
|
| 372 |
const char * scaleCommand; |
| 373 |
int termStatus; |
| 374 |
|
| 375 |
switch (PNM_FORMAT_TYPE(format)) { |
| 376 |
case PBM_TYPE: |
| 377 |
pm_asprintf(&scaleCommand, |
| 378 |
"pamscale -quiet -xysize %u %u " |
| 379 |
"| pamditherbw", |
| 380 |
size, size); |
| 381 |
break; |
| 382 |
|
| 383 |
case PGM_TYPE: |
| 384 |
pm_asprintf(&scaleCommand, |
| 385 |
"pamscale -quiet -xysize %u %u", |
| 386 |
size, size); |
| 387 |
break; |
| 388 |
|
| 389 |
case PPM_TYPE: |
| 390 |
if (quant) |
| 391 |
pm_asprintf(&scaleCommand, |
| 392 |
"pamscale -quiet -xysize %u %u " |
| 393 |
"| pnmquant -quiet %u ", |
| 394 |
size, size, colorCt); |
| 395 |
else |
| 396 |
pm_asprintf(&scaleCommand, |
| 397 |
"pamscale -quiet -xysize %u %u ", |
| 398 |
size, size); |
| 399 |
break; |
| 400 |
default: |
| 401 |
pm_error("Unrecognized Netpbm format: %d", format); |
| 402 |
} |
| 403 |
|
| 404 |
pm_system2(pm_feed_from_file, (void*)inputFileNm, |
| 405 |
pm_accept_to_file, (void*)outputFileNm, |
| 406 |
scaleCommand, |
| 407 |
&termStatus); |
| 408 |
|
| 409 |
if (termStatus != 0) |
| 410 |
pm_message("Shell command '%s' failed. Termination status=%d", |
| 411 |
scaleCommand, termStatus); |
| 412 |
|
| 413 |
pm_strfree(scaleCommand); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
static int |
| 419 |
formatTypeMax(int const typeA, |
| 420 |
int const typeB) { |
| 421 |
|
| 422 |
if (typeA == PPM_TYPE || typeB == PPM_TYPE) |
| 423 |
return PPM_TYPE; |
| 424 |
else if (typeA == PGM_TYPE || typeB == PGM_TYPE) |
| 425 |
return PGM_TYPE; |
| 426 |
else |
| 427 |
return PBM_TYPE; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
static const char * |
| 433 |
thumbnailFileName(const char * const dirName, |
| 434 |
unsigned int const row, |
| 435 |
unsigned int const col) { |
| 436 |
|
| 437 |
const char * fileName; |
| 438 |
|
| 439 |
pm_asprintf(&fileName, "%s/pi.%u.%u", dirName, row, col); |
| 440 |
|
| 441 |
return fileName; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static const char * |
| 447 |
thumbnailFileList(const char * const dirName, |
| 448 |
unsigned int const row, |
| 449 |
unsigned int const cols) { |
| 450 |
|
| 451 |
unsigned int const maxListSize = 4096; |
| 452 |
|
| 453 |
char * list; |
| 454 |
unsigned int col; |
| 455 |
|
| 456 |
list = malloc(maxListSize); |
| 457 |
if (list == NULL) |
| 458 |
pm_error("Unable to allocate %u bytes for file list", maxListSize); |
| 459 |
|
| 460 |
list[0] = '\0'; |
| 461 |
|
| 462 |
for (col = 0; col < cols; ++col) { |
| 463 |
const char * const fileName = thumbnailFileName(dirName, row, col); |
| 464 |
|
| 465 |
if (strlen(list) + strlen(fileName) + 1 > maxListSize - 1) |
| 466 |
pm_error("File name list too long for this program to handle."); |
| 467 |
else { |
| 468 |
strcat(list, " "); |
| 469 |
strcat(list, fileName); |
| 470 |
} |
| 471 |
pm_strfree(fileName); |
| 472 |
} |
| 473 |
|
| 474 |
return list; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static void |
| 480 |
makeImageFile(const char * const thumbnailFileNm, |
| 481 |
const char * const inputFileNm, |
| 482 |
bool const blackBackground, |
| 483 |
const char * const outputFileNm) { |
| 484 |
/*---------------------------------------------------------------------------- |
| 485 |
Create one thumbnail image. It consists of the image in the file named |
| 486 |
'thumbnailFileName' with text of that name appended to the bottom. |
| 487 |
|
| 488 |
Write the result to the file named 'outputFileName'. |
| 489 |
|
| 490 |
'blackBackground' means give the image a black background where padding |
| 491 |
is necessary and make the text white on black. If false, give the image |
| 492 |
a white background instead. |
| 493 |
-----------------------------------------------------------------------------*/ |
| 494 |
const char * const blackWhiteOpt = blackBackground ? "-black" : "-white"; |
| 495 |
const char * const invertStage = blackBackground ? "| pnminvert " : ""; |
| 496 |
const char * const thumbnailFileNmToken = shellQuote(thumbnailFileNm); |
| 497 |
|
| 498 |
struct bufferDesc fileNmBuf; |
| 499 |
const char * shellCommand; |
| 500 |
int termStatus; |
| 501 |
|
| 502 |
fileNmBuf.size = strlen(inputFileNm); |
| 503 |
fileNmBuf.buffer = (unsigned char *)inputFileNm; |
| 504 |
fileNmBuf.bytesTransferredP = NULL; |
| 505 |
|
| 506 |
pm_asprintf(&shellCommand, |
| 507 |
"pbmtext " |
| 508 |
"%s" |
| 509 |
"| pamcat %s -topbottom %s - ", |
| 510 |
invertStage, blackWhiteOpt, thumbnailFileNmToken); |
| 511 |
|
| 512 |
pm_system2(&pm_feed_from_memory, &fileNmBuf, |
| 513 |
&pm_accept_to_file, (void*)outputFileNm, |
| 514 |
shellCommand, |
| 515 |
&termStatus); |
| 516 |
|
| 517 |
if (termStatus != 0) |
| 518 |
pm_error("Shell command '%s' to add file name to thumbnail image " |
| 519 |
"of file '%s' failed, termination Status = %d", |
| 520 |
shellCommand, inputFileNm, termStatus); |
| 521 |
|
| 522 |
pm_strfree(thumbnailFileNmToken); |
| 523 |
pm_strfree(shellCommand); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
static void |
| 529 |
makeThumbnail(const char * const inputFileName, |
| 530 |
unsigned int const size, |
| 531 |
bool const black, |
| 532 |
bool const quant, |
| 533 |
unsigned int const colorCt, |
| 534 |
const char * const tempDir, |
| 535 |
unsigned int const row, |
| 536 |
unsigned int const col, |
| 537 |
int * const formatP) { |
| 538 |
|
| 539 |
const char * const fileName = thumbnailFileName(tempDir, row, col); |
| 540 |
|
| 541 |
FILE * ifP; |
| 542 |
int imageCols, imageRows, format; |
| 543 |
xelval maxval; |
| 544 |
const char * tmpfile; |
| 545 |
|
| 546 |
ifP = pm_openr(inputFileName); |
| 547 |
pnm_readpnminit(ifP, &imageCols, &imageRows, &maxval, &format); |
| 548 |
pm_close(ifP); |
| 549 |
|
| 550 |
pm_asprintf(&tmpfile, "%s/pi.tmp", tempDir); |
| 551 |
|
| 552 |
if (imageCols < size && imageRows < size) |
| 553 |
copyImage(inputFileName, tmpfile); |
| 554 |
else |
| 555 |
copyScaleQuantImage(inputFileName, tmpfile, format, |
| 556 |
size, quant, colorCt); |
| 557 |
|
| 558 |
makeImageFile(tmpfile, inputFileName, black, fileName); |
| 559 |
|
| 560 |
unlink(tmpfile); |
| 561 |
|
| 562 |
pm_strfree(fileName); |
| 563 |
pm_strfree(tmpfile); |
| 564 |
|
| 565 |
*formatP = format; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
static void |
| 571 |
unlinkThumbnailFiles(const char * const dirName, |
| 572 |
unsigned int const row, |
| 573 |
unsigned int const cols) { |
| 574 |
|
| 575 |
unsigned int col; |
| 576 |
|
| 577 |
for (col = 0; col < cols; ++col) { |
| 578 |
const char * const fileName = thumbnailFileName(dirName, row, col); |
| 579 |
|
| 580 |
unlink(fileName); |
| 581 |
|
| 582 |
pm_strfree(fileName); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
static void |
| 589 |
unlinkRowFiles(const char * const dirName, |
| 590 |
unsigned int const rows) { |
| 591 |
|
| 592 |
unsigned int row; |
| 593 |
|
| 594 |
for (row = 0; row < rows; ++row) { |
| 595 |
const char * const fileName = rowFileName(dirName, row); |
| 596 |
|
| 597 |
unlink(fileName); |
| 598 |
|
| 599 |
pm_strfree(fileName); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static void |
| 606 |
combineIntoRowAndDelete(unsigned int const row, |
| 607 |
unsigned int const colCt, |
| 608 |
int const maxFormatType, |
| 609 |
bool const blackBackground, |
| 610 |
bool const quant, |
| 611 |
unsigned int const colorCt, |
| 612 |
const char * const tempDir) { |
| 613 |
/*---------------------------------------------------------------------------- |
| 614 |
Combine the 'colCt' thumbnails for row 'row' into a PNM file in directory |
| 615 |
'tempDir'. |
| 616 |
|
| 617 |
Each thumbnail is from a specially named file in 'tempDir' whose name |
| 618 |
indicates its position in the row, and the output is also specially named |
| 619 |
with a name indicating it is row 'row'. |
| 620 |
|
| 621 |
Where the thumnails are different heights, pad with black if |
| 622 |
'blackBackground' is true; white otherwise. |
| 623 |
|
| 624 |
If 'quant', color-quantize the result to have no more than 'colorCt' |
| 625 |
colors, choosing the colors that best represent all the pixels in the |
| 626 |
result. |
| 627 |
|
| 628 |
'maxFormatType' is the most expressive format of all the thumbnail files; |
| 629 |
results are undefined if it is not. |
| 630 |
-----------------------------------------------------------------------------*/ |
| 631 |
const char * const blackWhiteOpt = blackBackground ? "-black" : "-white"; |
| 632 |
const char * const fileNm = rowFileName(tempDir, row); |
| 633 |
|
| 634 |
const char * quantStage; |
| 635 |
const char * fileList; |
| 636 |
const char * shellCommand; |
| 637 |
int termStatus; |
| 638 |
|
| 639 |
unlink(fileNm); |
| 640 |
|
| 641 |
if (maxFormatType == PPM_TYPE && quant) |
| 642 |
pm_asprintf(&quantStage, "| pnmquant -quiet %u ", colorCt); |
| 643 |
else |
| 644 |
quantStage = strdup(""); |
| 645 |
|
| 646 |
fileList = thumbnailFileList(tempDir, row, colCt); |
| 647 |
|
| 648 |
pm_asprintf(&shellCommand, "pamcat %s -leftright -jbottom %s " |
| 649 |
"%s", |
| 650 |
blackWhiteOpt, fileList, quantStage); |
| 651 |
|
| 652 |
pm_system2(&pm_feed_null, NULL, |
| 653 |
&pm_accept_to_file, (void*)fileNm, |
| 654 |
shellCommand, |
| 655 |
&termStatus); |
| 656 |
|
| 657 |
if (termStatus != 0) |
| 658 |
pm_error("Shell command '%s' to create row of thumbnails failed. " |
| 659 |
"Termination status = %d", shellCommand, termStatus); |
| 660 |
|
| 661 |
pm_strfree(fileList); |
| 662 |
pm_strfree(quantStage); |
| 663 |
pm_strfree(fileNm); |
| 664 |
pm_strfree(shellCommand); |
| 665 |
|
| 666 |
unlinkThumbnailFiles(tempDir, row, colCt); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
static const char * |
| 672 |
rowFileList(const char * const dirName, |
| 673 |
unsigned int const rows) { |
| 674 |
|
| 675 |
unsigned int const maxListSize = 4096; |
| 676 |
|
| 677 |
unsigned int row; |
| 678 |
char * list; |
| 679 |
|
| 680 |
list = malloc(maxListSize); |
| 681 |
if (list == NULL) |
| 682 |
pm_error("Unable to allocate %u bytes for file list", maxListSize); |
| 683 |
|
| 684 |
list[0] = '\0'; |
| 685 |
|
| 686 |
for (row = 0; row < rows; ++row) { |
| 687 |
const char * const fileName = rowFileName(dirName, row); |
| 688 |
|
| 689 |
if (strlen(list) + strlen(fileName) + 1 > maxListSize - 1) |
| 690 |
pm_error("File name list too long for this program to handle."); |
| 691 |
|
| 692 |
else { |
| 693 |
strcat(list, " "); |
| 694 |
strcat(list, fileName); |
| 695 |
} |
| 696 |
pm_strfree(fileName); |
| 697 |
} |
| 698 |
|
| 699 |
return list; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
static void |
| 705 |
writeRowsAndDelete(unsigned int const rowCt, |
| 706 |
int const maxFormatType, |
| 707 |
bool const blackBackground, |
| 708 |
bool const quant, |
| 709 |
unsigned int const colorCt, |
| 710 |
const char * const tempDir) { |
| 711 |
/*---------------------------------------------------------------------------- |
| 712 |
Write the PNM image containing the 'rowCt' rows of thumbnails to Standard |
| 713 |
Output. Take each row of thumbnails from a specially named PNM file in |
| 714 |
directory 'tempDir', and unlink it from that directory. |
| 715 |
|
| 716 |
Where the rows are different widths, pad with black if 'blackBackground' |
| 717 |
is true; white otherwise. |
| 718 |
|
| 719 |
If 'quant', color-quantize the result to have no more than 'colorCt' |
| 720 |
colors, choosing the colors that best represent all the pixels in the |
| 721 |
result. |
| 722 |
|
| 723 |
'maxFormatType' is the most expressive format of all the row files; results |
| 724 |
are undefined if it is not. |
| 725 |
-----------------------------------------------------------------------------*/ |
| 726 |
const char * const blackWhiteOpt = blackBackground ? "-black" : "-white"; |
| 727 |
const char * const plainOpt = pm_plain_output ? "-plain" : ""; |
| 728 |
|
| 729 |
const char * quantStage; |
| 730 |
const char * fileList; |
| 731 |
const char * shellCommand; |
| 732 |
int termStatus; |
| 733 |
|
| 734 |
if (maxFormatType == PPM_TYPE && quant) |
| 735 |
pm_asprintf(&quantStage, "| pnmquant -quiet %u ", colorCt); |
| 736 |
else |
| 737 |
quantStage = strdup(""); |
| 738 |
|
| 739 |
fileList = rowFileList(tempDir, rowCt); |
| 740 |
|
| 741 |
pm_asprintf(&shellCommand, "pamcat %s %s -topbottom %s %s", |
| 742 |
plainOpt, blackWhiteOpt, fileList, quantStage); |
| 743 |
|
| 744 |
/* Do pamcat/pnmquant command with no Standard Input and writing to |
| 745 |
our Standard Output |
| 746 |
*/ |
| 747 |
pm_system2(&pm_feed_null, NULL, |
| 748 |
NULL, NULL, |
| 749 |
shellCommand, |
| 750 |
&termStatus); |
| 751 |
|
| 752 |
if (termStatus != 0) |
| 753 |
pm_error("Shell command '%s' to assemble %u rows of thumbnails and " |
| 754 |
"write them out failed; termination status = %d", |
| 755 |
shellCommand, rowCt, termStatus); |
| 756 |
|
| 757 |
pm_strfree(shellCommand); |
| 758 |
pm_strfree(fileList); |
| 759 |
pm_strfree(quantStage); |
| 760 |
|
| 761 |
unlinkRowFiles(tempDir, rowCt); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
int |
| 767 |
main(int argc, const char ** argv) { |
| 768 |
|
| 769 |
struct CmdlineInfo cmdline; |
| 770 |
const char * tempDir; |
| 771 |
int maxFormatType; |
| 772 |
unsigned int colsInRow; |
| 773 |
unsigned int rowsDone; |
| 774 |
unsigned int i; |
| 775 |
|
| 776 |
pm_proginit(&argc, argv); |
| 777 |
|
| 778 |
parseCommandLine(argc, argv, &cmdline); |
| 779 |
|
| 780 |
verbose = cmdline.verbose; |
| 781 |
|
| 782 |
makeTempDir(&tempDir); |
| 783 |
|
| 784 |
rowsDone = 0; /* initial value */ |
| 785 |
|
| 786 |
if (cmdline.title) |
| 787 |
makeTitle(cmdline.title, rowsDone++, cmdline.black, tempDir); |
| 788 |
|
| 789 |
for (i = 0, colsInRow = 0, maxFormatType = PBM_TYPE; |
| 790 |
i < cmdline.inputFileCount; |
| 791 |
++i) { |
| 792 |
|
| 793 |
const char * const inputFileName = cmdline.inputFileName[i]; |
| 794 |
|
| 795 |
int format; |
| 796 |
|
| 797 |
makeThumbnail(inputFileName, cmdline.size, cmdline.black, |
| 798 |
!cmdline.noquant, cmdline.colors, tempDir, |
| 799 |
rowsDone, colsInRow, &format); |
| 800 |
|
| 801 |
maxFormatType = formatTypeMax(maxFormatType, PNM_FORMAT_TYPE(format)); |
| 802 |
|
| 803 |
++colsInRow; |
| 804 |
if (colsInRow >= cmdline.across || i == cmdline.inputFileCount-1) { |
| 805 |
combineIntoRowAndDelete( |
| 806 |
rowsDone, colsInRow, maxFormatType, |
| 807 |
cmdline.black, !cmdline.noquant, cmdline.colors, |
| 808 |
tempDir); |
| 809 |
++rowsDone; |
| 810 |
colsInRow = 0; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
writeRowsAndDelete(rowsDone, maxFormatType, cmdline.black, |
| 815 |
!cmdline.noquant, cmdline.colors, tempDir); |
| 816 |
|
| 817 |
removeTempDir(tempDir); |
| 818 |
|
| 819 |
freeCmdline(cmdline); |
| 820 |
|
| 821 |
pm_close(stdout); |
| 822 |
|
| 823 |
return 0; |
| 824 |
} |
| 825 |
|
| 826 |
|