| 1 |
/* ppmtoxpm.c - read a PPM image and produce a (version 3) X11 pixmap |
| 2 |
** |
| 3 |
** Copyright (C) 1990 by Mark W. Snitily |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
** |
| 12 |
** This tool was developed for Schlumberger Technologies, ATE Division, and |
| 13 |
** with their permission is being made available to the public with the above |
| 14 |
** copyright notice and permission notice. |
| 15 |
** |
| 16 |
** Upgraded to XPM2 by |
| 17 |
** Paul Breslaw, Mecasoft SA, Zurich, Switzerland ([email protected]) |
| 18 |
** Thu Nov 8 16:01:17 1990 |
| 19 |
** |
| 20 |
** Upgraded to XPM version 3 by |
| 21 |
** Arnaud Le Hors ([email protected]) |
| 22 |
** Tue Apr 9 1991 |
| 23 |
** |
| 24 |
** Rainer Sinkwitz [email protected] - 21 Nov 91: |
| 25 |
** - Bug fix, should should malloc space for rgbn[j].name+1 in line 441 |
| 26 |
** caused segmentation faults |
| 27 |
** |
| 28 |
** - lowercase conversion of RGB names def'ed out, |
| 29 |
** considered harmful. |
| 30 |
** |
| 31 |
** Michael Pall ([email protected]) - 29 Nov 93: |
| 32 |
** - Use the algorithm from xpm-lib for pixel encoding |
| 33 |
** (base 93 not base 28 -> saves a lot of space for colorful xpms) |
| 34 |
*/ |
| 35 |
|
| 36 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 37 |
#define _BSD_SOURCE /* Make sure strdup() is in string.h */ |
| 38 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 39 |
|
| 40 |
#include <stdbool.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <ctype.h> |
| 43 |
#include <string.h> |
| 44 |
|
| 45 |
#include "pm_c_util.h" |
| 46 |
#include "ppm.h" |
| 47 |
#include "shhopt.h" |
| 48 |
#include "nstring.h" |
| 49 |
#include "mallocvar.h" |
| 50 |
|
| 51 |
/* Max number of entries we will put in the XPM color map |
| 52 |
Don't forget the one entry for transparency. |
| 53 |
|
| 54 |
We don't use this anymore. Ppmtoxpm now has no arbitrary limit on |
| 55 |
the number of colors. |
| 56 |
|
| 57 |
We're assuming this isn't in fact an XPM format requirement, because |
| 58 |
we've seen it work with 257, and 257 seems to be common, because it's |
| 59 |
the classic 256 colors, plus transparency. The value was 256 for |
| 60 |
ages before we added transparency capability to this program in May |
| 61 |
2001. At that time, we started failing with 256 color images. |
| 62 |
Some limit was also necessary before then because ppm_computecolorhash() |
| 63 |
required us to specify a maximum number of colors. It doesn't anymore. |
| 64 |
|
| 65 |
If we find out that some XPM processing programs choke on more than |
| 66 |
256 colors, we'll have to readdress this issue. - Bryan. 2001.05.13. |
| 67 |
*/ |
| 68 |
#define MAXCOLORS 256 |
| 69 |
|
| 70 |
#define MAXPRINTABLE 92 /* number of printable ascii chars |
| 71 |
* minus \ and " for string compat |
| 72 |
* and ? to avoid ANSI trigraphs. */ |
| 73 |
|
| 74 |
static const char * const printable = |
| 75 |
" .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZ\ |
| 76 |
ASDFGHJKLPIUYTREWQ!~^/()_`'][{}|"; |
| 77 |
|
| 78 |
|
| 79 |
struct CmdlineInfo { |
| 80 |
/* All the information the user supplied in the command line, |
| 81 |
in a form easy for the program to use. |
| 82 |
*/ |
| 83 |
const char * inputFilename; |
| 84 |
const char * name; |
| 85 |
const char * rgb; |
| 86 |
const char * alphaFilename; |
| 87 |
unsigned int hexonly; |
| 88 |
unsigned int verbose; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static void |
| 94 |
parseCommandLine(int argc, const char ** argv, |
| 95 |
struct CmdlineInfo * const cmdlineP) { |
| 96 |
/*---------------------------------------------------------------------------- |
| 97 |
Note that the file spec array we return is stored in the storage that |
| 98 |
was passed to us as the argv array. |
| 99 |
-----------------------------------------------------------------------------*/ |
| 100 |
optEntry * option_def; |
| 101 |
optStruct3 opt; |
| 102 |
|
| 103 |
unsigned int option_def_index; |
| 104 |
const char * nameOpt; |
| 105 |
unsigned int nameSpec; |
| 106 |
|
| 107 |
MALLOCARRAY(option_def, 100); |
| 108 |
|
| 109 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 110 |
OPTENT3(0, "alphamask", OPT_STRING, &cmdlineP->alphaFilename, |
| 111 |
NULL, 0); |
| 112 |
OPTENT3(0, "name", OPT_STRING, &nameOpt, |
| 113 |
&nameSpec, 0); |
| 114 |
OPTENT3(0, "rgb", OPT_STRING, &cmdlineP->rgb, |
| 115 |
NULL, 0); |
| 116 |
OPTENT3(0, "hexonly", OPT_FLAG, NULL, |
| 117 |
&cmdlineP->hexonly, 0); |
| 118 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 119 |
&cmdlineP->verbose, 0); |
| 120 |
|
| 121 |
/* Set the defaults */ |
| 122 |
cmdlineP->alphaFilename = NULL; /* no transparency */ |
| 123 |
cmdlineP->rgb = NULL; /* no rgb file specified */ |
| 124 |
|
| 125 |
opt.opt_table = option_def; |
| 126 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 127 |
opt.allowNegNum = false; /* We may have parms that are negative numbers */ |
| 128 |
|
| 129 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 130 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 131 |
|
| 132 |
if (argc-1 == 0) |
| 133 |
cmdlineP->inputFilename = "-"; |
| 134 |
else if (argc-1 != 1) |
| 135 |
pm_error("Program takes zero or one argument (filename). You " |
| 136 |
"specified %d", argc-1); |
| 137 |
else |
| 138 |
cmdlineP->inputFilename = argv[1]; |
| 139 |
|
| 140 |
/* If output filename not specified, use input filename as default. */ |
| 141 |
if (nameSpec) |
| 142 |
cmdlineP->name = nameOpt; |
| 143 |
else if (streq(cmdlineP->inputFilename, "-")) |
| 144 |
cmdlineP->name = "noname"; |
| 145 |
else { |
| 146 |
static char name[80+1]; |
| 147 |
char *cp; |
| 148 |
|
| 149 |
STRSCPY(name, cmdlineP->inputFilename); |
| 150 |
cp = strchr(name, '.'); |
| 151 |
if (cp) |
| 152 |
*cp = '\0'; /* remove extension */ |
| 153 |
cmdlineP->name = name; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
typedef struct { |
| 160 |
/* Information for an XPM color map entry */ |
| 161 |
char *cixel; |
| 162 |
/* XPM color number, as might appear in the XPM raster */ |
| 163 |
const char *rgbname; |
| 164 |
/* color name, e.g. "blue" or "#01FF23" */ |
| 165 |
} CixelMap; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
static char * |
| 170 |
numstr(unsigned int const input, |
| 171 |
unsigned int const digitCt) { |
| 172 |
/*--------------------------------------------------------------------------- |
| 173 |
Given a number and a base (MAXPRINTABLE), this routine prints the |
| 174 |
number into a malloc'ed string and returns it. The length of the |
| 175 |
string is specified by 'digitCt'. It is not printed in decimal or |
| 176 |
any other number system you're used to. Rather, each digit is from |
| 177 |
the set printable[], which contains MAXPRINTABLE characters; the |
| 178 |
character printable[n] has value n. |
| 179 |
|
| 180 |
The string is printable[0] filled with high order zeroes. |
| 181 |
|
| 182 |
Example: |
| 183 |
Assume: |
| 184 |
printable[0] == 'q' |
| 185 |
printable[1] == '%' |
| 186 |
MAXPRINTABLE == 2 |
| 187 |
digitCt == 5 |
| 188 |
input == 3 |
| 189 |
Result is the malloc'ed string "qqq%%" |
| 190 |
---------------------------------------------------------------------------*/ |
| 191 |
char * str; /* malloc'ed */ |
| 192 |
char * p; |
| 193 |
unsigned int i; |
| 194 |
|
| 195 |
/* Allocate memory for printed number. Abort if error. */ |
| 196 |
MALLOCARRAY_NOFAIL(str, digitCt + 1); |
| 197 |
|
| 198 |
i = input; /* initial value */ |
| 199 |
/* Generate characters starting with least significant digit. */ |
| 200 |
p = str + digitCt; /* initial value */ |
| 201 |
*p-- = '\0'; /* nul terminate string */ |
| 202 |
while (p >= str) { |
| 203 |
unsigned int const d = i % MAXPRINTABLE; |
| 204 |
|
| 205 |
i /= MAXPRINTABLE; |
| 206 |
*p-- = printable[d]; |
| 207 |
} |
| 208 |
|
| 209 |
if (i != 0) |
| 210 |
pm_error("Overflow converting %u to %u digits in base %u", |
| 211 |
input, digitCt, MAXPRINTABLE); |
| 212 |
|
| 213 |
return str; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
static unsigned int |
| 219 |
xpmMaxvalFromMaxval(pixval const maxval) { |
| 220 |
|
| 221 |
unsigned int retval; |
| 222 |
|
| 223 |
/* Determine how many hex digits we'll be normalizing to if the rgb |
| 224 |
value doesn't match a color mnemonic. |
| 225 |
*/ |
| 226 |
if (maxval <= 0x000F) |
| 227 |
retval = 0x000F; |
| 228 |
else if (maxval <= 0x00FF) |
| 229 |
retval = 0x00FF; |
| 230 |
else if (maxval <= 0x0FFF) |
| 231 |
retval = 0x0FFF; |
| 232 |
else if (maxval <= 0xFFFF) |
| 233 |
retval = 0xFFFF; |
| 234 |
else |
| 235 |
pm_error("Internal error - impossible maxval %x", maxval); |
| 236 |
|
| 237 |
return retval; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
static unsigned int |
| 243 |
charsPerPixelForSize(unsigned int const cmapSize) { |
| 244 |
/*---------------------------------------------------------------------------- |
| 245 |
The number of characters it will take to represent a pixel in an XPM that |
| 246 |
has a colormap of size 'cmapSize'. Each pixel in an XPM represents an |
| 247 |
index into the colormap with a base-92 scheme where each character is one |
| 248 |
of 92 printable characters. Ergo, if the colormap has at most 92 |
| 249 |
characters, each pixel will be represented by a single character. If it |
| 250 |
has more than 92, but at most 92*92, it will take 2, etc. |
| 251 |
|
| 252 |
If cmapSize is zero, there's no such thing as an XPM pixel, so we return an |
| 253 |
undefined value. |
| 254 |
-----------------------------------------------------------------------------*/ |
| 255 |
unsigned int charsPerPixel; |
| 256 |
|
| 257 |
if (cmapSize > 0) { |
| 258 |
unsigned int j; |
| 259 |
|
| 260 |
for (charsPerPixel = 0, j = cmapSize-1; j > 0; ++charsPerPixel) |
| 261 |
j /= MAXPRINTABLE; |
| 262 |
} |
| 263 |
return charsPerPixel; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static void |
| 269 |
genCmap(colorhist_vector const chv, |
| 270 |
unsigned int const ncolors, |
| 271 |
pixval const maxval, |
| 272 |
ppm_ColorDict * const colorDictP, |
| 273 |
bool const includeTransparent, |
| 274 |
CixelMap ** const cmapP, |
| 275 |
unsigned int * const transIndexP, |
| 276 |
unsigned int * const cmapSizeP, |
| 277 |
unsigned int * const charsPerPixelP) { |
| 278 |
/*---------------------------------------------------------------------------- |
| 279 |
Generate the XPM color map in CixelMap format (which is just a step |
| 280 |
away from the actual text that needs to be written the XPM file). The |
| 281 |
color map is defined by 'chv', which contains 'ncolors' colors which |
| 282 |
have maxval 'maxval'. |
| 283 |
|
| 284 |
Output is in newly malloc'ed storage, with its address returned as |
| 285 |
*cmapP. We return the number of entries in it as *cmapSizeP. |
| 286 |
|
| 287 |
This map includes an entry for transparency, whether the raster uses |
| 288 |
it or not. We return its index as *transIndexP. |
| 289 |
|
| 290 |
In the map, identify colors by the names given by *colorDictP. If a color |
| 291 |
is not in *colorDictP, use hexadecimal notation in the output colormap. |
| 292 |
|
| 293 |
But if *colorDictP is null, don't use color names at all. Just use |
| 294 |
hexadecimal notation. |
| 295 |
|
| 296 |
Return as *charsPerPixel the number of characters, or digits, that |
| 297 |
will be needed in the XPM raster to form an index into this color map. |
| 298 |
-----------------------------------------------------------------------------*/ |
| 299 |
unsigned int const cmapSize = ncolors + (includeTransparent ? 1 : 0); |
| 300 |
|
| 301 |
CixelMap * cmap; /* malloc'ed */ |
| 302 |
unsigned int cmapIndex; |
| 303 |
unsigned int charsPerPixel; |
| 304 |
unsigned int xpmMaxval; |
| 305 |
|
| 306 |
if (includeTransparent) overflow_add(ncolors, 1); |
| 307 |
MALLOCARRAY(cmap, cmapSize); |
| 308 |
if (cmapP == NULL) |
| 309 |
pm_error("Can't get memory for a %u-entry color map", cmapSize); |
| 310 |
|
| 311 |
xpmMaxval = xpmMaxvalFromMaxval(maxval); |
| 312 |
|
| 313 |
charsPerPixel = charsPerPixelForSize(cmapSize); |
| 314 |
|
| 315 |
/* |
| 316 |
* Generate the character-pixel string and the rgb name for each |
| 317 |
* colormap entry. |
| 318 |
*/ |
| 319 |
for (cmapIndex = 0; cmapIndex < ncolors; ++cmapIndex) { |
| 320 |
pixel const color = chv[cmapIndex].color; |
| 321 |
|
| 322 |
pixel color255; |
| 323 |
/* The color, scaled to maxval 255 */ |
| 324 |
const char * colorname; /* malloc'ed */ |
| 325 |
/* |
| 326 |
* The character-pixel string is simply a printed number in base |
| 327 |
* MAXPRINTABLE where the digits of the number range from |
| 328 |
* printable[0] .. printable[MAXPRINTABLE-1] and the printed length |
| 329 |
* of the number is 'charsPerPixel'. |
| 330 |
*/ |
| 331 |
cmap[cmapIndex].cixel = numstr(cmapIndex, charsPerPixel); |
| 332 |
|
| 333 |
PPM_DEPTH(color255, color, maxval, 255); |
| 334 |
|
| 335 |
if (!colorDictP) |
| 336 |
colorname = NULL; |
| 337 |
else { |
| 338 |
int colornameIndex; |
| 339 |
colornameIndex = ppm_lookupcolor(colorDictP->cht, &color255); |
| 340 |
if (colornameIndex >= 0) |
| 341 |
colorname = strdup(colorDictP->name[colornameIndex]); |
| 342 |
else |
| 343 |
colorname = NULL; |
| 344 |
} |
| 345 |
if (colorname) |
| 346 |
cmap[cmapIndex].rgbname = colorname; |
| 347 |
else { |
| 348 |
/* Color has no name; represent it in hexadecimal */ |
| 349 |
|
| 350 |
pixel scaledColor; |
| 351 |
const char * hexString; /* malloc'ed */ |
| 352 |
|
| 353 |
PPM_DEPTH(scaledColor, color, maxval, xpmMaxval); |
| 354 |
|
| 355 |
pm_asprintf(&hexString, xpmMaxval == 0x000F ? "#%X%X%X" : |
| 356 |
xpmMaxval == 0x00FF ? "#%02X%02X%02X" : |
| 357 |
xpmMaxval == 0x0FFF ? "#%03X%03X%03X" : |
| 358 |
"#%04X%04X%04X", |
| 359 |
PPM_GETR(scaledColor), |
| 360 |
PPM_GETG(scaledColor), |
| 361 |
PPM_GETB(scaledColor) |
| 362 |
); |
| 363 |
|
| 364 |
if (hexString == NULL) |
| 365 |
pm_error("Unable to allocate storage for hex string"); |
| 366 |
cmap[cmapIndex].rgbname = hexString; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (includeTransparent) { |
| 371 |
/* Add the special transparency entry to the colormap */ |
| 372 |
unsigned int const transIndex = ncolors; |
| 373 |
cmap[transIndex].rgbname = strdup("None"); |
| 374 |
cmap[transIndex].cixel = numstr(transIndex, charsPerPixel); |
| 375 |
*transIndexP = transIndex; |
| 376 |
} |
| 377 |
*cmapP = cmap; |
| 378 |
*cmapSizeP = cmapSize; |
| 379 |
*charsPerPixelP = charsPerPixel; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static void |
| 385 |
destroyCmap(CixelMap * const cmap, |
| 386 |
unsigned int const cmapSize) { |
| 387 |
|
| 388 |
unsigned int i; |
| 389 |
|
| 390 |
/* Free the real color entries */ |
| 391 |
for (i = 0; i < cmapSize; ++i) { |
| 392 |
pm_strfree(cmap[i].rgbname); |
| 393 |
free(cmap[i].cixel); |
| 394 |
} |
| 395 |
|
| 396 |
free(cmap); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static void |
| 402 |
writeXpmFile(FILE * const ofP, |
| 403 |
pixel ** const pixels, |
| 404 |
gray ** const alpha, |
| 405 |
pixval const alphamaxval, |
| 406 |
char const name[], |
| 407 |
unsigned int const cols, |
| 408 |
unsigned int const rows, |
| 409 |
unsigned int const cmapSize, |
| 410 |
unsigned int const charsPerPixel, |
| 411 |
const CixelMap * const cmap, |
| 412 |
colorhash_table const cht, |
| 413 |
unsigned int const transIndex) { |
| 414 |
/*---------------------------------------------------------------------------- |
| 415 |
Write the whole XPM file to the open stream 'ofP'. |
| 416 |
|
| 417 |
'cmap' is the colormap to be placed in the XPM. 'cmapSize' is the number |
| 418 |
of entries in it. 'cht' is a hash table that gives you an index into |
| 419 |
'cmap' given a color. 'transIndex' is the index into cmap of the |
| 420 |
transparent color, and is valid only if 'alpha' is non-null (otherwise, |
| 421 |
cmap might not contain a transparent color). |
| 422 |
-----------------------------------------------------------------------------*/ |
| 423 |
/* First the header */ |
| 424 |
printf("/* XPM */\n"); |
| 425 |
fprintf(ofP, "static char *%s[] = {\n", name); |
| 426 |
fprintf(ofP, "/* width height ncolors chars_per_pixel */\n"); |
| 427 |
fprintf(ofP, "\"%u %u %u %u\",\n", cols, rows, cmapSize, charsPerPixel); |
| 428 |
|
| 429 |
{ |
| 430 |
unsigned int i; |
| 431 |
/* Write out the colormap (part of header) */ |
| 432 |
fprintf(ofP, "/* colors */\n"); |
| 433 |
for (i = 0; i < cmapSize; i++) { |
| 434 |
fprintf(ofP, "\"%s c %s\",\n", cmap[i].cixel, cmap[i].rgbname); |
| 435 |
} |
| 436 |
} |
| 437 |
{ |
| 438 |
unsigned int row; |
| 439 |
|
| 440 |
/* And now the raster */ |
| 441 |
fprintf(ofP, "/* pixels */\n"); |
| 442 |
for (row = 0; row < rows; ++row) { |
| 443 |
unsigned int col; |
| 444 |
fprintf(ofP, "\""); |
| 445 |
for (col = 0; col < cols; ++col) { |
| 446 |
if (alpha && alpha[row][col] <= alphamaxval/2) |
| 447 |
/* It's a transparent pixel */ |
| 448 |
fprintf(ofP, "%s", cmap[transIndex].cixel); |
| 449 |
else |
| 450 |
fprintf(ofP, "%s", |
| 451 |
cmap[ppm_lookupcolor(cht, |
| 452 |
&pixels[row][col])].cixel); |
| 453 |
} |
| 454 |
fprintf(ofP, "\"%s\n", (row == (rows - 1) ? "" : ",")); |
| 455 |
} |
| 456 |
} |
| 457 |
/* And close up */ |
| 458 |
fprintf(ofP, "};\n"); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
static void |
| 464 |
readAlpha(const char * const fileNm, |
| 465 |
gray *** const alphaP, |
| 466 |
unsigned int const cols, |
| 467 |
unsigned int const rows, |
| 468 |
pixval * const alphamaxvalP) { |
| 469 |
|
| 470 |
FILE * alphaFileP; |
| 471 |
int alphacols, alpharows; |
| 472 |
|
| 473 |
alphaFileP = pm_openr(fileNm); |
| 474 |
*alphaP = pgm_readpgm(alphaFileP, &alphacols, &alpharows, alphamaxvalP); |
| 475 |
pm_close(alphaFileP); |
| 476 |
|
| 477 |
if (cols != alphacols || rows != alpharows) |
| 478 |
pm_error("Alpha mask is not the same dimensions as the " |
| 479 |
"image. Image is %u by %u, while mask is %d x %d.", |
| 480 |
cols, rows, alphacols, alpharows); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
static void |
| 486 |
computecolorhash(pixel ** const pixels, |
| 487 |
gray ** const alpha, |
| 488 |
unsigned int const cols, |
| 489 |
unsigned int const rows, |
| 490 |
gray const alphaMaxval, |
| 491 |
colorhash_table * const chtP, |
| 492 |
unsigned int * const ncolorsP, |
| 493 |
bool * const transparentSomewhereP) { |
| 494 |
/*---------------------------------------------------------------------------- |
| 495 |
Compute a colorhash_table with one entry for each color in 'pixels' that |
| 496 |
is not mostly transparent according to alpha mask 'alpha' (which has |
| 497 |
maxval 'alphaMaxval'). alpha == NULL means all pixels are opaque. |
| 498 |
|
| 499 |
The value associated with the color in the hash we build is meaningless. |
| 500 |
|
| 501 |
Return the colorhash_table as *chtP, and the number of colors in it |
| 502 |
as *ncolorsP. Return *transparentSomewhereP == true iff the image has |
| 503 |
at least one pixel that is mostly transparent. |
| 504 |
-----------------------------------------------------------------------------*/ |
| 505 |
colorhash_table cht; |
| 506 |
unsigned int row; |
| 507 |
bool foundTransparent; |
| 508 |
unsigned int ncolors; |
| 509 |
|
| 510 |
cht = ppm_alloccolorhash(); |
| 511 |
|
| 512 |
/* Go through the entire image, building a hash table of colors. */ |
| 513 |
for (row = 0, ncolors = 0, foundTransparent = false; row < rows; ++row) { |
| 514 |
unsigned int col; |
| 515 |
|
| 516 |
for (col = 0; col < cols; ++col) { |
| 517 |
if (!alpha || alpha[row][col] > alphaMaxval/2) { |
| 518 |
/* It's mostly opaque, so add this color to the hash |
| 519 |
if it's not already there. |
| 520 |
*/ |
| 521 |
pixel const color = pixels[row][col]; |
| 522 |
int const lookupRc = ppm_lookupcolor(cht, &color); |
| 523 |
|
| 524 |
if (lookupRc < 0) { |
| 525 |
/* It's not in the hash yet, so add it */ |
| 526 |
if (ncolors > UINT_MAX - 10) |
| 527 |
pm_error("Number of colors (> %u) " |
| 528 |
"is uncomputably large", |
| 529 |
ncolors); |
| 530 |
ppm_addtocolorhash(cht, &color, 0); |
| 531 |
++ncolors; |
| 532 |
} |
| 533 |
} else |
| 534 |
*transparentSomewhereP = true; |
| 535 |
} |
| 536 |
} |
| 537 |
*chtP = cht; |
| 538 |
*ncolorsP = ncolors; |
| 539 |
*transparentSomewhereP = foundTransparent; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
static void |
| 545 |
computeColormap(pixel ** const pixels, |
| 546 |
gray ** const alpha, |
| 547 |
unsigned int const cols, |
| 548 |
unsigned int const rows, |
| 549 |
gray const alphaMaxval, |
| 550 |
colorhist_vector * const chvP, |
| 551 |
colorhash_table * const chtP, |
| 552 |
unsigned int * const ncolorsP, |
| 553 |
bool * const transparentSomewhereP) { |
| 554 |
/*---------------------------------------------------------------------------- |
| 555 |
Compute the color map for the image 'pixels', which is 'cols' by 'rows', |
| 556 |
in Netpbm data structures (a colorhist_vector for index-to-color lookups |
| 557 |
and a colorhash_table for color-to-index lookups). |
| 558 |
|
| 559 |
Exclude pixels that alpha mask 'alpha' (which has maxval 'alphaMaxval') |
| 560 |
says are mostly transparent. alpha == NULL means all pixels are opaque. |
| 561 |
|
| 562 |
We return as *chvP an array of the colors present in 'pixels', excluding |
| 563 |
those that are mostly transparent. We return as *ncolorsP the number of |
| 564 |
such colors. We return *transparentSomewhereP == true iff the image has at |
| 565 |
least one pixel that is mostly transparent. |
| 566 |
-----------------------------------------------------------------------------*/ |
| 567 |
colorhash_table histCht; |
| 568 |
|
| 569 |
pm_message("(Computing colormap..."); |
| 570 |
computecolorhash(pixels, alpha, cols, rows, alphaMaxval, |
| 571 |
&histCht, ncolorsP, transparentSomewhereP); |
| 572 |
pm_message("...Done. %d colors found.)", *ncolorsP); |
| 573 |
|
| 574 |
*chvP = ppm_colorhashtocolorhist(histCht, *ncolorsP); |
| 575 |
ppm_freecolorhash(histCht); |
| 576 |
/* Despite the name, the following generates an index on *chvP, |
| 577 |
with which given a color you can quickly find the entry number |
| 578 |
in *chvP that contains that color. |
| 579 |
*/ |
| 580 |
*chtP = ppm_colorhisttocolorhash(*chvP, *ncolorsP); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
int |
| 586 |
main(int argc, const char * *argv) { |
| 587 |
|
| 588 |
FILE * ifP; |
| 589 |
int rows, cols; |
| 590 |
unsigned int ncolors; |
| 591 |
bool transparentSomewhere; |
| 592 |
pixval maxval, alphaMaxval; |
| 593 |
colorhash_table cht; |
| 594 |
colorhist_vector chv; |
| 595 |
|
| 596 |
ppm_ColorDict * colorDictP; /* The color name dictionary we use */ |
| 597 |
|
| 598 |
pixel ** pixels; |
| 599 |
gray ** alpha; |
| 600 |
|
| 601 |
/* Used for rgb value -> character-pixel string mapping */ |
| 602 |
CixelMap * cmap; /* malloc'ed */ |
| 603 |
/* The XPM colormap */ |
| 604 |
unsigned int cmapSize; |
| 605 |
/* Number of entries in 'cmap' */ |
| 606 |
unsigned int transIndex; |
| 607 |
/* Index into 'cmap' of the transparent color, if there is one */ |
| 608 |
|
| 609 |
unsigned int charsPerPixel; |
| 610 |
|
| 611 |
struct CmdlineInfo cmdline; |
| 612 |
|
| 613 |
pm_proginit(&argc, argv); |
| 614 |
|
| 615 |
parseCommandLine(argc, argv, &cmdline); |
| 616 |
|
| 617 |
ifP = pm_openr(cmdline.inputFilename); |
| 618 |
pixels = ppm_readppm(ifP, &cols, &rows, &maxval); |
| 619 |
pm_close(ifP); |
| 620 |
|
| 621 |
if (cmdline.alphaFilename) |
| 622 |
readAlpha(cmdline.alphaFilename, &alpha, cols, rows, &alphaMaxval); |
| 623 |
else |
| 624 |
alpha = NULL; |
| 625 |
|
| 626 |
computeColormap(pixels, alpha, cols, rows, alphaMaxval, |
| 627 |
&chv, &cht, &ncolors, &transparentSomewhere); |
| 628 |
|
| 629 |
if (cmdline.hexonly) |
| 630 |
colorDictP = NULL; |
| 631 |
else if (cmdline.rgb) |
| 632 |
colorDictP = ppm_colorDict_new(cmdline.rgb, true); |
| 633 |
else |
| 634 |
colorDictP = ppm_colorDict_new(NULL, false); |
| 635 |
|
| 636 |
/* Now generate the character-pixel colormap table. */ |
| 637 |
genCmap(chv, ncolors, maxval, colorDictP, transparentSomewhere, |
| 638 |
&cmap, &transIndex, &cmapSize, &charsPerPixel); |
| 639 |
|
| 640 |
writeXpmFile(stdout, pixels, alpha, alphaMaxval, |
| 641 |
cmdline.name, cols, rows, cmapSize, |
| 642 |
charsPerPixel, cmap, cht, transIndex); |
| 643 |
|
| 644 |
if (colorDictP) |
| 645 |
ppm_colorDict_destroy(colorDictP); |
| 646 |
destroyCmap(cmap, cmapSize); |
| 647 |
ppm_freearray(pixels, rows); |
| 648 |
if (alpha) |
| 649 |
pgm_freearray(alpha, rows); |
| 650 |
|
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|