| 1 |
/* pamtouil.c - convert PBM, PGM, PPM, or PPM+alpha to Motif UIL icon file |
| 2 |
** |
| 3 |
** Bryan Henderson converted ppmtouil to pamtouil on 2002.05.04. |
| 4 |
** |
| 5 |
** Jef Poskanzer derived pamtouil from ppmtoxpm, which is |
| 6 |
** Copyright (C) 1990 by Mark W. Snitily. |
| 7 |
** |
| 8 |
** Permission to use, copy, modify, and distribute this software and its |
| 9 |
** documentation for any purpose and without fee is hereby granted, provided |
| 10 |
** that the above copyright notice appear in all copies and that both that |
| 11 |
** copyright notice and this permission notice appear in supporting |
| 12 |
** documentation. This software is provided "as is" without express or |
| 13 |
** implied warranty. |
| 14 |
*/ |
| 15 |
|
| 16 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 17 |
#define _BSD_SOURCE /* Make sure string.h contains strdup() */ |
| 18 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 19 |
#include <ctype.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "pm_c_util.h" |
| 23 |
#include "pam.h" |
| 24 |
#include "pammap.h" |
| 25 |
#include "colorname.h" |
| 26 |
#include "shhopt.h" |
| 27 |
#include "nstring.h" |
| 28 |
|
| 29 |
/* Max number of colors allowed in ppm input. */ |
| 30 |
#define MAXCOLORS 256 |
| 31 |
|
| 32 |
/* Lower bound and upper bound of character-pixels printed in UIL output. */ |
| 33 |
#define LOW_CHAR '`' |
| 34 |
#define HIGH_CHAR '~' |
| 35 |
|
| 36 |
struct cmdlineInfo { |
| 37 |
/* All the information the user supplied in the command line, |
| 38 |
in a form easy for the program to use. |
| 39 |
*/ |
| 40 |
const char * inputFilespec; /* Filespecs of input files */ |
| 41 |
char * outname; /* output filename, less "_icon" */ |
| 42 |
unsigned int verbose; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
typedef struct { /* character-pixel mapping */ |
| 48 |
const char * cixel; /* character string printed for pixel */ |
| 49 |
const char * rgbname; /* ascii rgb color, either mnemonic or #rgb value */ |
| 50 |
const char * uilname; /* same, with spaces replaced by underbars */ |
| 51 |
bool transparent; |
| 52 |
} cixel_map; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static void |
| 57 |
parseCommandLine(int argc, char ** argv, |
| 58 |
struct cmdlineInfo * const cmdlineP) { |
| 59 |
/*---------------------------------------------------------------------------- |
| 60 |
Note that the file spec array we return is stored in the storage that |
| 61 |
was passed to us as the argv array. The outname array is in newly |
| 62 |
malloc'ed storage. |
| 63 |
-----------------------------------------------------------------------------*/ |
| 64 |
optEntry *option_def = malloc( 100*sizeof( optEntry ) ); |
| 65 |
/* Instructions to pm_optParseOptions3 on how to parse our options. |
| 66 |
*/ |
| 67 |
optStruct3 opt; |
| 68 |
|
| 69 |
unsigned int option_def_index; |
| 70 |
unsigned int outnameSpec; |
| 71 |
const char *outnameOpt; |
| 72 |
|
| 73 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 74 |
OPTENT3(0, "name", OPT_STRING, &outnameOpt, |
| 75 |
&outnameSpec, 0); |
| 76 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 77 |
&cmdlineP->verbose, 0); |
| 78 |
|
| 79 |
opt.opt_table = option_def; |
| 80 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 81 |
opt.allowNegNum = FALSE; /* We may have parms that are negative numbers */ |
| 82 |
|
| 83 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 84 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 85 |
|
| 86 |
if (argc-1 == 0) |
| 87 |
cmdlineP->inputFilespec = "-"; /* stdin */ |
| 88 |
else if (argc-1 == 1) |
| 89 |
cmdlineP->inputFilespec = argv[1]; |
| 90 |
else |
| 91 |
pm_error("Too many arguments (%d). " |
| 92 |
"Only need one: the input filespec", argc-1); |
| 93 |
|
| 94 |
if (outnameSpec) { |
| 95 |
char * barPos; |
| 96 |
|
| 97 |
cmdlineP->outname = strdup(outnameOpt); |
| 98 |
|
| 99 |
/* Remove trailing "_icon" */ |
| 100 |
barPos = strrchr(cmdlineP->outname, '_'); |
| 101 |
if (barPos && streq(barPos, "_icon")) |
| 102 |
*barPos = '\0'; |
| 103 |
} else { |
| 104 |
if (streq(cmdlineP->inputFilespec, "-")) |
| 105 |
cmdlineP->outname = strdup("noname"); |
| 106 |
else { |
| 107 |
char * dotPos; |
| 108 |
|
| 109 |
cmdlineP->outname = strdup(cmdlineP->inputFilespec); |
| 110 |
|
| 111 |
/* remove extension */ |
| 112 |
dotPos = strrchr(cmdlineP->outname, '.'); |
| 113 |
if (dotPos) |
| 114 |
*dotPos = '\0'; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
static char* |
| 122 |
genNumstr(int const number, |
| 123 |
int const base, |
| 124 |
char const low_char, |
| 125 |
int const digits ) { |
| 126 |
/*---------------------------------------------------------------------------- |
| 127 |
Generate a string 'digits' characters long in newly malloc'ed |
| 128 |
storage that is the number 'number' displayed in base 'base'. Fill it on |
| 129 |
the left with zero digits and truncate on the left if it doesn't fit. |
| 130 |
|
| 131 |
Use the characters 'low_char' to 'low_char' + 'base' to represent the |
| 132 |
digits 0 to 'base'. |
| 133 |
-----------------------------------------------------------------------------*/ |
| 134 |
char* str; |
| 135 |
char* p; |
| 136 |
int d; |
| 137 |
int i; |
| 138 |
|
| 139 |
/* Allocate memory for printed number. Abort if error. */ |
| 140 |
str = (char*) malloc(digits + 1); |
| 141 |
if (str == NULL) |
| 142 |
pm_error("out of memory allocating number string"); |
| 143 |
|
| 144 |
/* Generate characters starting with least significant digit. */ |
| 145 |
i = number; |
| 146 |
p = str + digits; |
| 147 |
*p-- = '\0'; /* nul terminate string */ |
| 148 |
while (p >= str) { |
| 149 |
d = i % base; |
| 150 |
i /= base; |
| 151 |
*p-- = low_char + d; |
| 152 |
} |
| 153 |
return str; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static const char * |
| 159 |
uilName(const char * const rgbname, |
| 160 |
bool const transparent) { |
| 161 |
/*---------------------------------------------------------------------------- |
| 162 |
Return a string in newly malloc'ed storage which is an appropriate |
| 163 |
color name for the UIL palette. It is the same as the rgb name, |
| 164 |
except that blanks are replaced by underscores, and if 'transparent' |
| 165 |
is true, it is "background color". The latter is a strange name of |
| 166 |
a color, but it works pretty much the same in the UIL colortable() value. |
| 167 |
-----------------------------------------------------------------------------*/ |
| 168 |
char * output; |
| 169 |
|
| 170 |
if (transparent) |
| 171 |
output = strdup("background color"); |
| 172 |
else { |
| 173 |
int i; |
| 174 |
|
| 175 |
output = malloc(strlen(rgbname) + 5 + 1); |
| 176 |
if (output == NULL) |
| 177 |
pm_error( "out of memory allocating color name" ); |
| 178 |
|
| 179 |
for (i = 0; i < strlen(rgbname); ++i) { |
| 180 |
if (rgbname[i] == ' ') |
| 181 |
output[i] = '_'; |
| 182 |
else |
| 183 |
output[i] = rgbname[i]; |
| 184 |
} |
| 185 |
output[strlen(rgbname)] = '\0'; |
| 186 |
} |
| 187 |
|
| 188 |
return output; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static void |
| 194 |
genCmap(struct pam * const pamP, |
| 195 |
tupletable const chv, |
| 196 |
unsigned int const ncolors, |
| 197 |
cixel_map cmap[MAXCOLORS], |
| 198 |
unsigned int * const charsppP, |
| 199 |
bool const verbose) { |
| 200 |
|
| 201 |
unsigned int const base = (int) HIGH_CHAR - (int) LOW_CHAR + 1; |
| 202 |
char* colorname; |
| 203 |
unsigned int colorIndex; |
| 204 |
{ |
| 205 |
/* Figure out how many characters per pixel we'll be using. |
| 206 |
Don't want to be forced to link with libm.a, so using a |
| 207 |
division loop rather than a log function. |
| 208 |
*/ |
| 209 |
unsigned int i; |
| 210 |
for (*charsppP = 0, i = ncolors; i > 0; ++(*charsppP)) |
| 211 |
i /= base; |
| 212 |
} |
| 213 |
|
| 214 |
/* Generate the character-pixel string and the rgb name for each colormap |
| 215 |
entry. |
| 216 |
*/ |
| 217 |
for (colorIndex = 0; colorIndex < ncolors; ++colorIndex) { |
| 218 |
bool nameAlreadyInCmap; |
| 219 |
unsigned int indexOfName; |
| 220 |
bool transparent; |
| 221 |
int j; |
| 222 |
|
| 223 |
if (pamP->depth-1 < PAM_TRN_PLANE) |
| 224 |
transparent = FALSE; |
| 225 |
else |
| 226 |
transparent = |
| 227 |
chv[colorIndex]->tuple[PAM_TRN_PLANE] < pamP->maxval/2; |
| 228 |
|
| 229 |
/* Generate color name string. */ |
| 230 |
colorname = pam_colorname(pamP, chv[colorIndex]->tuple, |
| 231 |
PAM_COLORNAME_ENGLISH); |
| 232 |
|
| 233 |
/* We may have already assigned a character code to this color |
| 234 |
name/transparency because the same color name can apply to |
| 235 |
two different colors because we said we wanted the closest |
| 236 |
matching color that has an English name, and we recognize |
| 237 |
only one transparent color. If that's the case, we just |
| 238 |
make a cross-reference. |
| 239 |
*/ |
| 240 |
nameAlreadyInCmap = FALSE; /* initial assumption */ |
| 241 |
for (j = 0; j < colorIndex; ++j) { |
| 242 |
if (cmap[j].rgbname != NULL && |
| 243 |
streq(colorname, cmap[j].rgbname) && |
| 244 |
cmap[j].transparent == transparent) { |
| 245 |
nameAlreadyInCmap = TRUE; |
| 246 |
indexOfName = j; |
| 247 |
} |
| 248 |
} |
| 249 |
if (nameAlreadyInCmap) { |
| 250 |
/* Make the entry a cross-reference to the earlier entry */ |
| 251 |
cmap[colorIndex].uilname = NULL; |
| 252 |
cmap[colorIndex].rgbname = NULL; |
| 253 |
cmap[colorIndex].cixel = cmap[indexOfName].cixel; |
| 254 |
} else { |
| 255 |
cmap[colorIndex].uilname = uilName(colorname, transparent); |
| 256 |
cmap[colorIndex].rgbname = strdup(colorname); |
| 257 |
if (cmap[colorIndex].rgbname == NULL) |
| 258 |
pm_error("out of memory allocating color name"); |
| 259 |
|
| 260 |
cmap[colorIndex].transparent = transparent; |
| 261 |
|
| 262 |
/* Generate color value characters. */ |
| 263 |
cmap[colorIndex].cixel = |
| 264 |
genNumstr(colorIndex, base, LOW_CHAR, *charsppP); |
| 265 |
if (verbose) |
| 266 |
pm_message("Adding color '%s' %s = '%s' to UIL colormap", |
| 267 |
cmap[colorIndex].rgbname, |
| 268 |
cmap[colorIndex].transparent ? "TRANS" : "OPAQUE", |
| 269 |
cmap[colorIndex].cixel); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static void |
| 277 |
writeUilHeader(const char * const outname) { |
| 278 |
/* Write out the UIL header. */ |
| 279 |
printf( "module %s\n", outname ); |
| 280 |
printf( "version = 'V1.0'\n" ); |
| 281 |
printf( "names = case_sensitive\n" ); |
| 282 |
printf( "include file 'XmAppl.uil';\n" ); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static void |
| 288 |
writeColormap(const char * const outname, |
| 289 |
cixel_map cmap[MAXCOLORS], |
| 290 |
unsigned int const ncolors) { |
| 291 |
|
| 292 |
{ |
| 293 |
int i; |
| 294 |
|
| 295 |
/* Write out the colors. */ |
| 296 |
printf("\n"); |
| 297 |
printf("value\n"); |
| 298 |
for (i = 0; i < ncolors; ++i) |
| 299 |
if (cmap[i].uilname != NULL && !cmap[i].transparent) |
| 300 |
printf(" %s : color( '%s' );\n", |
| 301 |
cmap[i].uilname, cmap[i].rgbname ); |
| 302 |
} |
| 303 |
{ |
| 304 |
/* Write out the ascii colormap. */ |
| 305 |
|
| 306 |
int i; |
| 307 |
bool printedOne; |
| 308 |
|
| 309 |
printf("\n"); |
| 310 |
printf("value\n"); |
| 311 |
printf(" %s_rgb : color_table (\n", outname); |
| 312 |
printedOne = FALSE; |
| 313 |
for (i = 0; i < ncolors; ++i) |
| 314 |
if (cmap[i].uilname != NULL) { |
| 315 |
if (printedOne) |
| 316 |
printf(",\n"); |
| 317 |
printf(" %s = '%s'", cmap[i].uilname, cmap[i].cixel); |
| 318 |
printedOne = TRUE; |
| 319 |
} |
| 320 |
printf("\n"); |
| 321 |
printf(" );\n"); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static void |
| 328 |
writeRaster(struct pam * const pamP, |
| 329 |
tuple ** const tuples, |
| 330 |
const char * const outname, |
| 331 |
cixel_map cmap[MAXCOLORS], |
| 332 |
unsigned int const ncolors, |
| 333 |
tuplehash const cht, |
| 334 |
unsigned int const charspp) { |
| 335 |
/*---------------------------------------------------------------------------- |
| 336 |
Write out the ascii character-pixel image. |
| 337 |
-----------------------------------------------------------------------------*/ |
| 338 |
unsigned int row; |
| 339 |
|
| 340 |
printf("\n"); |
| 341 |
printf("%s_icon : exported icon( color_table = %s_rgb,\n", |
| 342 |
outname, outname); |
| 343 |
for (row = 0; row < pamP->height; ++row) { |
| 344 |
unsigned int col; |
| 345 |
|
| 346 |
printf(" '"); |
| 347 |
for (col = 0; col < pamP->width; ++col) { |
| 348 |
int colorIndex; |
| 349 |
int found; |
| 350 |
if ((col * charspp) % 70 == 0 && col > 0) |
| 351 |
printf( "\\\n" ); /* line continuation */ |
| 352 |
pnm_lookuptuple(pamP, cht, tuples[row][col], &found, &colorIndex); |
| 353 |
if (!found) |
| 354 |
pm_error("INTERNAL ERROR: color not found in colormap"); |
| 355 |
printf("%s", cmap[colorIndex].cixel); |
| 356 |
} |
| 357 |
if (row != pamP->height - 1) |
| 358 |
printf("',\n"); |
| 359 |
else |
| 360 |
printf("'\n"); |
| 361 |
} |
| 362 |
printf(");\n"); |
| 363 |
printf("\n"); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
static void |
| 369 |
freeCmap(cixel_map const cmap[], |
| 370 |
unsigned int const ncolors) { |
| 371 |
|
| 372 |
unsigned int i; |
| 373 |
|
| 374 |
for (i = 0; i < ncolors; ++i) { |
| 375 |
cixel_map const cmapEntry = cmap[i]; |
| 376 |
if (cmapEntry.uilname) |
| 377 |
pm_strfree(cmapEntry.uilname); |
| 378 |
if (cmapEntry.rgbname) |
| 379 |
pm_strfree(cmapEntry.rgbname); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
int |
| 386 |
main(int argc, char *argv[]) { |
| 387 |
|
| 388 |
struct cmdlineInfo cmdline; |
| 389 |
struct pam pam; /* Input PAM image */ |
| 390 |
FILE * ifP; |
| 391 |
tuple ** tuples; |
| 392 |
unsigned int ncolors; |
| 393 |
tuplehash cht; |
| 394 |
tupletable chv; |
| 395 |
cixel_map cmap[MAXCOLORS]; |
| 396 |
unsigned int charspp; |
| 397 |
|
| 398 |
pnm_init(&argc, argv); |
| 399 |
|
| 400 |
parseCommandLine(argc, argv, &cmdline); |
| 401 |
|
| 402 |
ifP = pm_openr(cmdline.inputFilespec); |
| 403 |
tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 404 |
pm_close(ifP); |
| 405 |
|
| 406 |
pm_message("computing colormap..."); |
| 407 |
|
| 408 |
chv = pnm_computetuplefreqtable(&pam, tuples, MAXCOLORS, &ncolors); |
| 409 |
if (chv == NULL) |
| 410 |
pm_error("too many colors - try doing a 'pnmquant %u'", MAXCOLORS); |
| 411 |
if (cmdline.verbose) |
| 412 |
pm_message("%u colors found", ncolors); |
| 413 |
|
| 414 |
/* Make a hash table for fast color lookup. */ |
| 415 |
cht = pnm_computetupletablehash(&pam, chv, ncolors); |
| 416 |
|
| 417 |
/* Now generate the character-pixel colormap table. */ |
| 418 |
pm_message("looking up color names, assigning character codes..."); |
| 419 |
genCmap(&pam, chv, ncolors, cmap, &charspp, cmdline.verbose); |
| 420 |
|
| 421 |
pm_message("generating UIL..."); |
| 422 |
writeUilHeader(cmdline.outname); |
| 423 |
|
| 424 |
writeColormap(cmdline.outname, cmap, ncolors); |
| 425 |
|
| 426 |
writeRaster(&pam, tuples, cmdline.outname, cmap, ncolors, cht, charspp); |
| 427 |
|
| 428 |
printf("end module;\n"); |
| 429 |
|
| 430 |
free(cmdline.outname); |
| 431 |
freeCmap(cmap, ncolors); |
| 432 |
|
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|