| 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 |
#include <assert.h> |
| 71 |
#include <string.h> |
| 72 |
|
| 73 |
#include "pm_c_util.h" |
| 74 |
#include "mallocvar.h" |
| 75 |
#include "ppm.h" |
| 76 |
#include "pbm.h" |
| 77 |
#include "runlength.h" |
| 78 |
#include "ilbm.h" |
| 79 |
#include "lum.h" |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#define MODE_RGB8 6 |
| 84 |
#define MODE_RGBN 5 |
| 85 |
#define MODE_CMAP 4 |
| 86 |
#define MODE_DCOL 3 |
| 87 |
#define MODE_DEEP 2 |
| 88 |
#define MODE_HAM 1 |
| 89 |
#define MODE_NONE 0 |
| 90 |
|
| 91 |
#define HAMMODE_GRAY 0 |
| 92 |
#define HAMMODE_FIXED 1 |
| 93 |
#define HAMMODE_MAPFILE 2 |
| 94 |
#define HAMMODE_RGB4 3 |
| 95 |
#define HAMMODE_RGB5 4 |
| 96 |
|
| 97 |
#define ECS_MAXPLANES 5 |
| 98 |
#define ECS_HAMPLANES 6 |
| 99 |
#define AGA_MAXPLANES 8 |
| 100 |
#define AGA_HAMPLANES 8 |
| 101 |
|
| 102 |
#define HAMMAXPLANES 10 |
| 103 |
|
| 104 |
#define DEF_MAXPLANES ECS_MAXPLANES |
| 105 |
#define DEF_HAMPLANES ECS_HAMPLANES |
| 106 |
#define DEF_COMPRESSION cmpByteRun1 |
| 107 |
#define DEF_DEEPPLANES 8 |
| 108 |
#define DEF_DCOLPLANES 5 |
| 109 |
#define DEF_IFMODE MODE_DEEP |
| 110 |
|
| 111 |
#define INT16MAX 32767 |
| 112 |
|
| 113 |
#define PAD(n) (ODD(n) ? 1 : 0) |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static unsigned char *coded_rowbuf; |
| 118 |
static unsigned char *compr_rowbuf; |
| 119 |
static pixel **pixels; |
| 120 |
static pixel *pixrow; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static long int viewportmodes = 0; |
| 126 |
static int slicesize = 1; |
| 127 |
|
| 128 |
|
| 129 |
static unsigned char compmethod = DEF_COMPRESSION; |
| 130 |
static unsigned char maskmethod = mskNone; |
| 131 |
|
| 132 |
static pixel *transpColor = NULL; |
| 133 |
static short transpIndex = -1; |
| 134 |
|
| 135 |
static short hammapmode = HAMMODE_GRAY; |
| 136 |
static short sortcmap = 0; |
| 137 |
|
| 138 |
static FILE *maskfile = NULL; |
| 139 |
static bit *maskrow = NULL; |
| 140 |
static int maskcols, maskformat; |
| 141 |
#define TOTALPLANES(nplanes) ((nplanes) + ((maskmethod == mskHasMask) ? 1 : 0)) |
| 142 |
|
| 143 |
|
| 144 |
#define ROWS_PER_BLOCK 1024 |
| 145 |
typedef struct bodyblock { |
| 146 |
int used; |
| 147 |
unsigned char *row[ROWS_PER_BLOCK]; |
| 148 |
int len[ROWS_PER_BLOCK]; |
| 149 |
struct bodyblock *next; |
| 150 |
} bodyblock; |
| 151 |
static bodyblock firstblock = { 0 }; |
| 152 |
static bodyblock *cur_block = &firstblock; |
| 153 |
|
| 154 |
static char *anno_chunk, *auth_chunk, *name_chunk, *text_chunk, *copyr_chunk; |
| 155 |
|
| 156 |
|
| 157 |
static short compr_force = 0; |
| 158 |
|
| 159 |
static short gen_camg = 0; |
| 160 |
|
| 161 |
#define WORSTCOMPR(bytes) ((bytes) + (bytes)/128 + 1) |
| 162 |
#define DO_COMPRESS (compmethod != cmpNone) |
| 163 |
|
| 164 |
#define NEWDEPTH(pix, table) PPM_ASSIGN((pix), (table)[PPM_GETR(pix)], (table)[PPM_GETG(pix)], (table)[PPM_GETB(pix)]) |
| 165 |
|
| 166 |
#define putByte(b) (void)(putc((unsigned char)(b), stdout)) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static void |
| 175 |
writeBytes(unsigned char * const buffer, |
| 176 |
int const bytes) { |
| 177 |
|
| 178 |
if( fwrite(buffer, 1, bytes, stdout) != bytes ) |
| 179 |
pm_error("write error"); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static int * |
| 185 |
makeValTable(int const oldmaxval, |
| 186 |
int const newmaxval) { |
| 187 |
|
| 188 |
unsigned int i; |
| 189 |
int * table; |
| 190 |
|
| 191 |
MALLOCARRAY_NOFAIL(table, oldmaxval + 1); |
| 192 |
for (i = 0; i <= oldmaxval; ++i) |
| 193 |
table[i] = ROUNDDIV(i * newmaxval, oldmaxval); |
| 194 |
|
| 195 |
return table; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static int gFormat; |
| 201 |
static int gCols; |
| 202 |
static int gMaxval; |
| 203 |
|
| 204 |
static void |
| 205 |
initRead(FILE * const fp, |
| 206 |
int * const colsP, |
| 207 |
int * const rowsP, |
| 208 |
pixval * const maxvalP, |
| 209 |
int * const formatP, |
| 210 |
int const readall) { |
| 211 |
|
| 212 |
ppm_readppminit(fp, colsP, rowsP, maxvalP, formatP); |
| 213 |
|
| 214 |
if( *rowsP >INT16MAX || *colsP >INT16MAX ) |
| 215 |
pm_error ("Input image is too large."); |
| 216 |
|
| 217 |
if( readall ) { |
| 218 |
int row; |
| 219 |
|
| 220 |
pixels = ppm_allocarray(*colsP, *rowsP); |
| 221 |
for( row = 0; row < *rowsP; row++ ) |
| 222 |
ppm_readppmrow(fp, pixels[row], *colsP, *maxvalP, *formatP); |
| 223 |
|
| 224 |
} |
| 225 |
else { |
| 226 |
pixrow = ppm_allocrow(*colsP); |
| 227 |
} |
| 228 |
gCols = *colsP; |
| 229 |
gMaxval = *maxvalP; |
| 230 |
gFormat = *formatP; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static pixel * |
| 236 |
nextPixrow(FILE * const fp, |
| 237 |
int const row) { |
| 238 |
|
| 239 |
if( pixels ) |
| 240 |
pixrow = pixels[row]; |
| 241 |
else { |
| 242 |
ppm_readppmrow(fp, pixrow, gCols, gMaxval, gFormat); |
| 243 |
} |
| 244 |
if( maskrow ) { |
| 245 |
int col; |
| 246 |
|
| 247 |
if( maskfile ) |
| 248 |
pbm_readpbmrow(maskfile, maskrow, maskcols, maskformat); |
| 249 |
else { |
| 250 |
for( col = 0; col < gCols; col++ ) |
| 251 |
maskrow[col] = PBM_BLACK; |
| 252 |
} |
| 253 |
if( transpColor ) { |
| 254 |
for( col = 0; col < gCols; col++ ) |
| 255 |
if( PPM_EQUAL(pixrow[col], *transpColor) ) |
| 256 |
maskrow[col] = PBM_WHITE; |
| 257 |
} |
| 258 |
} |
| 259 |
return pixrow; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static int |
| 269 |
lengthOfTextChunks(void) { |
| 270 |
|
| 271 |
int len, n; |
| 272 |
|
| 273 |
len = 0; |
| 274 |
if( anno_chunk ) { |
| 275 |
n = strlen(anno_chunk); |
| 276 |
len += 4 + 4 + n + PAD(n); |
| 277 |
} |
| 278 |
if( auth_chunk ) { |
| 279 |
n = strlen(auth_chunk); |
| 280 |
len += 4 + 4 + n + PAD(n); |
| 281 |
} |
| 282 |
if( name_chunk ) { |
| 283 |
n = strlen(name_chunk); |
| 284 |
len += 4 + 4 + n + PAD(n); |
| 285 |
} |
| 286 |
if( copyr_chunk ) { |
| 287 |
n = strlen(copyr_chunk); |
| 288 |
len += 4 + 4 + n + PAD(n); |
| 289 |
} |
| 290 |
if( text_chunk ) { |
| 291 |
n = strlen(text_chunk); |
| 292 |
len += 4 + 4 + n + PAD(n); |
| 293 |
} |
| 294 |
return len; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static void |
| 300 |
writeTextChunks(void) { |
| 301 |
|
| 302 |
int n; |
| 303 |
|
| 304 |
if( anno_chunk ) { |
| 305 |
n = strlen(anno_chunk); |
| 306 |
pm_writebiglong(stdout, ID_ANNO); |
| 307 |
pm_writebiglong(stdout, n); |
| 308 |
writeBytes((unsigned char *)anno_chunk, n); |
| 309 |
if( ODD(n) ) |
| 310 |
putByte(0); |
| 311 |
} |
| 312 |
if( auth_chunk ) { |
| 313 |
n = strlen(auth_chunk); |
| 314 |
pm_writebiglong(stdout, ID_AUTH); |
| 315 |
pm_writebiglong(stdout, n); |
| 316 |
writeBytes((unsigned char *)auth_chunk, n); |
| 317 |
if( ODD(n) ) |
| 318 |
putByte(0); |
| 319 |
} |
| 320 |
if( copyr_chunk ) { |
| 321 |
n = strlen(copyr_chunk); |
| 322 |
pm_writebiglong(stdout, ID_copy); |
| 323 |
pm_writebiglong(stdout, n); |
| 324 |
writeBytes((unsigned char *)copyr_chunk, n); |
| 325 |
if( ODD(n) ) |
| 326 |
putByte(0); |
| 327 |
} |
| 328 |
if( name_chunk ) { |
| 329 |
n = strlen(name_chunk); |
| 330 |
pm_writebiglong(stdout, ID_NAME); |
| 331 |
pm_writebiglong(stdout, n); |
| 332 |
writeBytes((unsigned char *)name_chunk, n); |
| 333 |
if( ODD(n) ) |
| 334 |
putByte(0); |
| 335 |
} |
| 336 |
if( text_chunk ) { |
| 337 |
n = strlen(text_chunk); |
| 338 |
pm_writebiglong(stdout, ID_TEXT); |
| 339 |
pm_writebiglong(stdout, n); |
| 340 |
writeBytes((unsigned char *)text_chunk, n); |
| 341 |
if( ODD(n) ) |
| 342 |
putByte(0); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static void |
| 349 |
writeCmap(pixel * const colormap, |
| 350 |
int const colors, |
| 351 |
int const maxval) { |
| 352 |
|
| 353 |
int cmapsize, i; |
| 354 |
|
| 355 |
cmapsize = 3 * colors; |
| 356 |
|
| 357 |
|
| 358 |
pm_writebiglong(stdout, ID_CMAP); |
| 359 |
pm_writebiglong(stdout, cmapsize); |
| 360 |
if( maxval != MAXCOLVAL ) { |
| 361 |
int *table; |
| 362 |
pm_message("maxval is not %d - automatically rescaling colors", |
| 363 |
MAXCOLVAL); |
| 364 |
table = makeValTable(maxval, MAXCOLVAL); |
| 365 |
for( i = 0; i < colors; i++ ) { |
| 366 |
putByte(table[PPM_GETR(colormap[i])]); |
| 367 |
putByte(table[PPM_GETG(colormap[i])]); |
| 368 |
putByte(table[PPM_GETB(colormap[i])]); |
| 369 |
} |
| 370 |
free(table); |
| 371 |
} |
| 372 |
else { |
| 373 |
for( i = 0; i < colors; i++ ) { |
| 374 |
putByte(PPM_GETR(colormap[i])); |
| 375 |
putByte(PPM_GETG(colormap[i])); |
| 376 |
putByte(PPM_GETB(colormap[i])); |
| 377 |
} |
| 378 |
} |
| 379 |
if( ODD(cmapsize) ) |
| 380 |
putByte(0); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static void |
| 386 |
writeBmhd(int const cols, |
| 387 |
int const rows, |
| 388 |
int const nPlanes) { |
| 389 |
|
| 390 |
unsigned char xasp, yasp; |
| 391 |
|
| 392 |
xasp = 10; |
| 393 |
yasp = 10; |
| 394 |
|
| 395 |
if( viewportmodes & vmLACE ) |
| 396 |
xasp *= 2; |
| 397 |
if( viewportmodes & vmHIRES ) |
| 398 |
yasp *= 2; |
| 399 |
|
| 400 |
pm_writebiglong(stdout, ID_BMHD); |
| 401 |
pm_writebiglong(stdout, BitMapHeaderSize); |
| 402 |
|
| 403 |
pm_writebigshort(stdout, cols); |
| 404 |
pm_writebigshort(stdout, rows); |
| 405 |
pm_writebigshort(stdout, 0); |
| 406 |
pm_writebigshort(stdout, 0); |
| 407 |
putByte(nPlanes); |
| 408 |
putByte(maskmethod); |
| 409 |
putByte(compmethod); |
| 410 |
putByte(BMHD_FLAGS_CMAPOK); |
| 411 |
if( maskmethod == mskHasTransparentColor ) |
| 412 |
pm_writebigshort(stdout, transpIndex); |
| 413 |
else |
| 414 |
pm_writebigshort(stdout, 0); |
| 415 |
putByte(xasp); |
| 416 |
putByte(yasp); |
| 417 |
pm_writebigshort(stdout, cols); |
| 418 |
pm_writebigshort(stdout, rows); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
static void |
| 426 |
storeBodyrow(unsigned char * const row, |
| 427 |
int const len) { |
| 428 |
|
| 429 |
int idx; |
| 430 |
|
| 431 |
idx = cur_block->used; |
| 432 |
|
| 433 |
if (idx >= ROWS_PER_BLOCK) { |
| 434 |
MALLOCVAR_NOFAIL(cur_block->next); |
| 435 |
cur_block = cur_block->next; |
| 436 |
cur_block->used = idx = 0; |
| 437 |
cur_block->next = NULL; |
| 438 |
} |
| 439 |
MALLOCARRAY_NOFAIL(cur_block->row[idx], len); |
| 440 |
cur_block->len[idx] = len; |
| 441 |
memcpy(cur_block->row[idx], row, len); |
| 442 |
++cur_block->used; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
static unsigned int |
| 448 |
compressRow(unsigned int const bytes) { |
| 449 |
|
| 450 |
size_t compressedByteCt; |
| 451 |
|
| 452 |
switch (compmethod) { |
| 453 |
case cmpByteRun1: |
| 454 |
pm_rlenc_compressbyte( |
| 455 |
coded_rowbuf, compr_rowbuf, PM_RLE_PACKBITS, bytes, |
| 456 |
&compressedByteCt); |
| 457 |
break; |
| 458 |
default: |
| 459 |
pm_error("compressRow(): unknown compression method %d", |
| 460 |
compmethod); |
| 461 |
} |
| 462 |
storeBodyrow(compr_rowbuf, compressedByteCt); |
| 463 |
|
| 464 |
assert((unsigned)compressedByteCt == compressedByteCt); |
| 465 |
|
| 466 |
return (unsigned)compressedByteCt; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
static const unsigned char bitmask[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
static long int |
| 476 |
encodeRow(FILE * const outfile, |
| 477 |
|
| 478 |
rawtype * const rawrow, |
| 479 |
int const cols, |
| 480 |
int const nPlanes) { |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
int plane, bytes; |
| 485 |
long int retbytes = 0; |
| 486 |
|
| 487 |
bytes = ilbm_rowByteCt(cols); |
| 488 |
|
| 489 |
|
| 490 |
for( plane = 0; plane < nPlanes; plane++ ) { |
| 491 |
int col, cbit; |
| 492 |
rawtype *rp; |
| 493 |
unsigned char *cp; |
| 494 |
int mask; |
| 495 |
|
| 496 |
mask = 1 << plane; |
| 497 |
cbit = -1; |
| 498 |
cp = coded_rowbuf-1; |
| 499 |
rp = rawrow; |
| 500 |
for( col = 0; col < cols; col++, cbit--, rp++ ) { |
| 501 |
if( cbit < 0 ) { |
| 502 |
cbit = 7; |
| 503 |
*++cp = 0; |
| 504 |
} |
| 505 |
if( *rp & mask ) |
| 506 |
*cp |= bitmask[cbit]; |
| 507 |
} |
| 508 |
if( outfile ) { |
| 509 |
writeBytes(coded_rowbuf, bytes); |
| 510 |
retbytes += bytes; |
| 511 |
} |
| 512 |
else |
| 513 |
retbytes += compressRow(bytes); |
| 514 |
} |
| 515 |
return retbytes; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
static long int |
| 521 |
encodeMaskrow(FILE * const ofP, |
| 522 |
rawtype * const rawrow, |
| 523 |
int const cols) { |
| 524 |
|
| 525 |
int col; |
| 526 |
|
| 527 |
for( col = 0; col < cols; col++ ) { |
| 528 |
if( maskrow[col] == PBM_BLACK ) |
| 529 |
rawrow[col] = 1; |
| 530 |
else |
| 531 |
rawrow[col] = 0; |
| 532 |
} |
| 533 |
return encodeRow(ofP, rawrow, cols, 1); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
static void |
| 539 |
writeCamg(void) { |
| 540 |
pm_writebiglong(stdout, ID_CAMG); |
| 541 |
pm_writebiglong(stdout, CAMGChunkSize); |
| 542 |
pm_writebiglong(stdout, viewportmodes); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
static void |
| 548 |
reportTooManyColors(int const ifmode, |
| 549 |
int const maxplanes, |
| 550 |
int const hamplanes, |
| 551 |
DirectColor const dcol, |
| 552 |
int const deepbits) { |
| 553 |
|
| 554 |
int const maxcolors = 1 << maxplanes; |
| 555 |
|
| 556 |
switch( ifmode ) { |
| 557 |
case MODE_HAM: |
| 558 |
pm_message("too many colors for %d planes - " |
| 559 |
"proceeding to write a HAM%d file", |
| 560 |
maxplanes, hamplanes); |
| 561 |
pm_message("if you want a non-HAM file, try doing a 'pnmquant %d'", |
| 562 |
maxcolors); |
| 563 |
break; |
| 564 |
case MODE_DCOL: |
| 565 |
pm_message("too many colors for %d planes - " |
| 566 |
"proceeding to write a %d:%d:%d direct color ILBM", |
| 567 |
maxplanes, dcol.r, dcol.g, dcol.b); |
| 568 |
pm_message("if you want a non-direct color file, " |
| 569 |
"try doing a 'pnmquant %d'", maxcolors); |
| 570 |
break; |
| 571 |
case MODE_DEEP: |
| 572 |
pm_message("too many colors for %d planes - " |
| 573 |
"proceeding to write a %d-bit \'deep\' ILBM", |
| 574 |
maxplanes, deepbits*3); |
| 575 |
pm_message("if you want a non-deep file, " |
| 576 |
"try doing a 'pnmquant %d'", |
| 577 |
maxcolors); |
| 578 |
break; |
| 579 |
default: |
| 580 |
pm_error("too many colors for %d planes - " |
| 581 |
"try doing a 'pnmquant %d'", |
| 582 |
maxplanes, maxcolors); |
| 583 |
break; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
static int |
| 590 |
getIntVal(const char * const string, |
| 591 |
const char * const option, |
| 592 |
int const bot, |
| 593 |
int const top) { |
| 594 |
|
| 595 |
int val; |
| 596 |
|
| 597 |
if (sscanf(string, "%d", &val) != 1 ) |
| 598 |
pm_error("option \"%s\" needs integer argument", option); |
| 599 |
|
| 600 |
if (val < bot || val > top) |
| 601 |
pm_error("option \"%s\" argument value out of range (%d..%d)", |
| 602 |
option, bot, top); |
| 603 |
|
| 604 |
return val; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
static int |
| 610 |
getComprMethod(const char * const string) { |
| 611 |
|
| 612 |
int retval; |
| 613 |
if( pm_keymatch(string, "none", 1) || pm_keymatch(string, "0", 1) ) |
| 614 |
retval = cmpNone; |
| 615 |
else if( pm_keymatch(string, "byterun1", 1) || |
| 616 |
pm_keymatch(string, "1", 1) ) |
| 617 |
retval = cmpByteRun1; |
| 618 |
else |
| 619 |
pm_error("unknown compression method: %s", string); |
| 620 |
return retval; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
static int |
| 626 |
getMaskType(const char * const string) { |
| 627 |
|
| 628 |
int retval; |
| 629 |
|
| 630 |
if( pm_keymatch(string, "none", 1) || pm_keymatch(string, "0", 1) ) |
| 631 |
retval = mskNone; |
| 632 |
else |
| 633 |
if( pm_keymatch(string, "plane", 1) || |
| 634 |
pm_keymatch(string, "maskplane", 1) || |
| 635 |
pm_keymatch(string, "1", 1) ) |
| 636 |
retval = mskHasMask; |
| 637 |
else |
| 638 |
if( pm_keymatch(string, "transparentcolor", 1) || |
| 639 |
pm_keymatch(string, "2", 1) ) |
| 640 |
retval = mskHasTransparentColor; |
| 641 |
else |
| 642 |
if( pm_keymatch(string, "lasso", 1) || pm_keymatch(string, "3", 1) ) |
| 643 |
retval = mskLasso; |
| 644 |
else |
| 645 |
pm_error("unknown masking method: %s", string); |
| 646 |
return retval; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
static int |
| 652 |
getHammapMode(const char * const string) { |
| 653 |
|
| 654 |
int retval; |
| 655 |
|
| 656 |
if( pm_keymatch(string, "grey", 1) || pm_keymatch(string, "gray", 1) ) |
| 657 |
retval = HAMMODE_GRAY; |
| 658 |
else |
| 659 |
if( pm_keymatch(string, "fixed", 1) ) |
| 660 |
retval = HAMMODE_FIXED; |
| 661 |
else |
| 662 |
if( pm_keymatch(string, "rgb4", 4) ) |
| 663 |
retval = HAMMODE_RGB4; |
| 664 |
else |
| 665 |
if( pm_keymatch(string, "rgb5", 4) ) |
| 666 |
retval = HAMMODE_RGB5; |
| 667 |
else |
| 668 |
pm_error("unknown HAM colormap selection mode: %s", string); |
| 669 |
return retval; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
static void |
| 679 |
ppmToCmap(pixel * const colorrow, |
| 680 |
int const colors, |
| 681 |
int const maxval) { |
| 682 |
|
| 683 |
int formsize, cmapsize; |
| 684 |
|
| 685 |
cmapsize = colors * 3; |
| 686 |
|
| 687 |
formsize = |
| 688 |
4 + |
| 689 |
4 + 4 + BitMapHeaderSize + |
| 690 |
4 + 4 + cmapsize + PAD(cmapsize) + |
| 691 |
lengthOfTextChunks(); |
| 692 |
|
| 693 |
pm_writebiglong(stdout, ID_FORM); |
| 694 |
pm_writebiglong(stdout, formsize); |
| 695 |
pm_writebiglong(stdout, ID_ILBM); |
| 696 |
|
| 697 |
writeBmhd(0, 0, 0); |
| 698 |
writeTextChunks(); |
| 699 |
writeCmap(colorrow, colors, maxval); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
typedef struct { |
| 709 |
long int count; |
| 710 |
pixval r, g, b; |
| 711 |
} hentry; |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
#ifndef LITERAL_FN_DEF_MATCH |
| 716 |
static qsort_comparison_fn hcmp; |
| 717 |
#endif |
| 718 |
|
| 719 |
static int |
| 720 |
hcmp(const void * const a, |
| 721 |
const void * const b) { |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
const hentry * const vaP = a; |
| 726 |
const hentry * const vbP = b; |
| 727 |
|
| 728 |
return(vbP->count - vaP->count); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
static pixel * |
| 734 |
computeHamCmap(int const cols, |
| 735 |
int const rows, |
| 736 |
int const maxval, |
| 737 |
int const maxcolors, |
| 738 |
int * const colorsP, |
| 739 |
int const hbits) { |
| 740 |
|
| 741 |
int colors; |
| 742 |
hentry *hmap; |
| 743 |
pixel *cmap; |
| 744 |
pixval hmaxval; |
| 745 |
int i, r, g, b, col, row, *htable; |
| 746 |
unsigned long int dist, maxdist; |
| 747 |
|
| 748 |
pm_message("initializing HAM colormap..."); |
| 749 |
|
| 750 |
colors = 1<<(3*hbits); |
| 751 |
MALLOCARRAY(hmap, colors); |
| 752 |
if (hmap == NULL) |
| 753 |
pm_error("Unable to allocate memory for HAM colormap."); |
| 754 |
hmaxval = pm_bitstomaxval(hbits); |
| 755 |
|
| 756 |
i = 0; |
| 757 |
for( r = 0; r <= hmaxval; r++ ) { |
| 758 |
for( g = 0; g <= hmaxval; g++ ) { |
| 759 |
for( b = 0; b <= hmaxval; b++ ) { |
| 760 |
hmap[i].r = r; hmap[i].g = g; hmap[i].b = b; |
| 761 |
hmap[i].count = 0; |
| 762 |
i++; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
htable = makeValTable(maxval, hmaxval); |
| 768 |
for( row = 0; row < rows; row++ ) { |
| 769 |
unsigned int col; |
| 770 |
for( col = 0; col < cols; ++col) { |
| 771 |
pixel const p = pixels[row][col]; |
| 772 |
pixval const r = PPM_GETR(p); |
| 773 |
pixval const g = PPM_GETG(p); |
| 774 |
pixval const b = PPM_GETB(p); |
| 775 |
i = (htable[r]<<(2*hbits)) + (htable[g]<<hbits) + htable[b]; |
| 776 |
hmap[i].count++; |
| 777 |
} |
| 778 |
} |
| 779 |
free(htable); |
| 780 |
|
| 781 |
qsort((void *)hmap, colors, sizeof(hentry), hcmp); |
| 782 |
for( i = colors-1; i >= 0; i-- ) { |
| 783 |
if( hmap[i].count ) |
| 784 |
break; |
| 785 |
} |
| 786 |
colors = i+1; |
| 787 |
|
| 788 |
if( colors > maxcolors ) { |
| 789 |
pm_message("selecting HAM colormap from %d colors...", colors); |
| 790 |
for( maxdist = 1; ; maxdist++ ) { |
| 791 |
for( col = colors-1; col > 0; col-- ) { |
| 792 |
r = hmap[col].r; g = hmap[col].g; b = hmap[col].b; |
| 793 |
for( i = 0; i < col; i++ ) { |
| 794 |
int tmp; |
| 795 |
|
| 796 |
tmp = hmap[i].r - r; dist = tmp * tmp; |
| 797 |
tmp = hmap[i].g - g; dist += tmp * tmp; |
| 798 |
tmp = hmap[i].b - b; dist += tmp * tmp; |
| 799 |
|
| 800 |
if( dist <= maxdist ) { |
| 801 |
unsigned int sum = hmap[i].count + hmap[col].count; |
| 802 |
|
| 803 |
hmap[i].r = ROUNDDIV(hmap[i].r * hmap[i].count + |
| 804 |
r * hmap[col].count, sum); |
| 805 |
hmap[i].g = ROUNDDIV(hmap[i].g * hmap[i].count + |
| 806 |
g * hmap[col].count, sum); |
| 807 |
hmap[i].b = ROUNDDIV(hmap[i].b * hmap[i].count + |
| 808 |
b * hmap[col].count, sum); |
| 809 |
hmap[i].count = sum; |
| 810 |
|
| 811 |
hmap[col] = hmap[i]; |
| 812 |
for( tmp = i-1; |
| 813 |
tmp >= 0 && hmap[tmp].count < hmap[col].count; |
| 814 |
tmp-- ) |
| 815 |
hmap[tmp+1] = hmap[tmp]; |
| 816 |
hmap[tmp+1] = hmap[col]; |
| 817 |
|
| 818 |
for( tmp = col; tmp < colors-1; tmp++ ) |
| 819 |
hmap[tmp] = hmap[tmp+1]; |
| 820 |
if( --colors <= maxcolors ) |
| 821 |
goto out; |
| 822 |
break; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
#ifdef DEBUG |
| 827 |
pm_message("\tmaxdist=%ld: %d colors left", maxdist, colors); |
| 828 |
#endif |
| 829 |
} |
| 830 |
} |
| 831 |
out: |
| 832 |
pm_message("%d colors in HAM colormap", colors); |
| 833 |
|
| 834 |
cmap = ppm_allocrow(colors); |
| 835 |
*colorsP = colors; |
| 836 |
|
| 837 |
for( i = 0; i < colors; i++ ) { |
| 838 |
r = hmap[i].r; g = hmap[i].g; b = hmap[i].b; |
| 839 |
PPM_ASSIGN(cmap[i], r, g, b); |
| 840 |
} |
| 841 |
|
| 842 |
ppm_freerow(hmap); |
| 843 |
return cmap; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static void |
| 849 |
writeBodyRows(void) { |
| 850 |
|
| 851 |
bodyblock *b; |
| 852 |
int i; |
| 853 |
long int total = 0; |
| 854 |
|
| 855 |
for( b = &firstblock; b != NULL; b = b->next ) { |
| 856 |
for( i = 0; i < b->used; i++ ) { |
| 857 |
writeBytes(b->row[i], b->len[i]); |
| 858 |
total += b->len[i]; |
| 859 |
} |
| 860 |
} |
| 861 |
if( ODD(total) ) |
| 862 |
putByte(0); |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
static long int |
| 868 |
doHamBody(FILE * const ifP, |
| 869 |
FILE * const ofP, |
| 870 |
int const cols, |
| 871 |
int const rows, |
| 872 |
pixval const maxval, |
| 873 |
pixval const hammaxval, |
| 874 |
int const nPlanes, |
| 875 |
pixel * const colormap, |
| 876 |
int const colors) { |
| 877 |
|
| 878 |
int col, row, i; |
| 879 |
rawtype *raw_rowbuf; |
| 880 |
colorhash_table cht, cht2; |
| 881 |
long int bodysize = 0; |
| 882 |
int *itoh; |
| 883 |
int usehash = 1; |
| 884 |
int colbits; |
| 885 |
int hamcode_red, hamcode_green, hamcode_blue; |
| 886 |
|
| 887 |
MALLOCARRAY_NOFAIL(raw_rowbuf, cols); |
| 888 |
|
| 889 |
cht = ppm_colorrowtocolorhash(colormap, colors); |
| 890 |
cht2 = ppm_alloccolorhash(); |
| 891 |
colbits = pm_maxvaltobits(hammaxval); |
| 892 |
|
| 893 |
hamcode_red = HAMCODE_RED << colbits; |
| 894 |
hamcode_green = HAMCODE_GREEN << colbits; |
| 895 |
hamcode_blue = HAMCODE_BLUE << colbits; |
| 896 |
|
| 897 |
itoh = makeValTable(maxval, hammaxval); |
| 898 |
|
| 899 |
for( row = 0; row < rows; row++ ) { |
| 900 |
int noprev; |
| 901 |
int spr, spg, spb; |
| 902 |
int upr, upg, upb; |
| 903 |
pixel *prow; |
| 904 |
|
| 905 |
noprev = 1; |
| 906 |
prow = nextPixrow(ifP, row); |
| 907 |
for( col = 0; col < cols; ++col ) { |
| 908 |
|
| 909 |
pixel const p = prow[col]; |
| 910 |
|
| 911 |
|
| 912 |
pixval const ur = PPM_GETR(p); |
| 913 |
pixval const ug = PPM_GETG(p); |
| 914 |
pixval const ub = PPM_GETB(p); |
| 915 |
|
| 916 |
|
| 917 |
int const sr = itoh[ur]; |
| 918 |
int const sg = itoh[ug]; |
| 919 |
int const sb = itoh[ub]; |
| 920 |
|
| 921 |
i = ppm_lookupcolor(cht, &p); |
| 922 |
if( i == -1 ) { |
| 923 |
int ucr, ucg, ucb; |
| 924 |
|
| 925 |
if( hammapmode == HAMMODE_GRAY ) { |
| 926 |
if( maxval <= 255 ) |
| 927 |
|
| 928 |
|
| 929 |
i = (int)ppm_fastlumin(p); |
| 930 |
else |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
i = (int)(PPM_LUMIN(p) + 0.5); |
| 935 |
|
| 936 |
i = itoh[i]; |
| 937 |
} |
| 938 |
else { |
| 939 |
i = ppm_lookupcolor(cht2, &p); |
| 940 |
if( i == -1 ) { |
| 941 |
i = ppm_findclosestcolor(colormap, colors, &p); |
| 942 |
if( usehash ) { |
| 943 |
if( ppm_addtocolorhash(cht2, &p, i) < 0 ) { |
| 944 |
pm_message("out of memory " |
| 945 |
"adding to hash table, " |
| 946 |
"proceeding without it"); |
| 947 |
usehash = 0; |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
ucr = PPM_GETR(colormap[i]); |
| 953 |
ucg = PPM_GETG(colormap[i]); |
| 954 |
ucb = PPM_GETB(colormap[i]); |
| 955 |
|
| 956 |
if( noprev ) { |
| 957 |
raw_rowbuf[col] = i; |
| 958 |
upr = ucr; upg = ucg; upb = ucb; |
| 959 |
spr = itoh[upr]; spg = itoh[upg]; spb = itoh[upb]; |
| 960 |
noprev = 0; |
| 961 |
} else { |
| 962 |
long int di, dr, dg, db; |
| 963 |
int scr, scg, scb; |
| 964 |
|
| 965 |
scr = itoh[ucr]; scg = itoh[ucg]; scb = itoh[ucb]; |
| 966 |
|
| 967 |
|
| 968 |
#if 1 |
| 969 |
dr = abs(sg - spg) + abs(sb - spb); |
| 970 |
dg = abs(sr - spr) + abs(sb - spb); |
| 971 |
db = abs(sr - spr) + abs(sg - spg); |
| 972 |
di = abs(sr - scr) + abs(sg - scg) + abs(sb - scb); |
| 973 |
#else |
| 974 |
dr = (sg - spg)*(sg - spg) + (sb - spb)*(sb - spb); |
| 975 |
dg = (sr - spr)*(sr - spr) + (sb - spb)*(sb - spb); |
| 976 |
db = (sr - spr)*(sr - spr) + (sg - spg)*(sg - spg); |
| 977 |
di = (sr - scr)*(sr - scr) + (sg - scg)*(sg - scg) + |
| 978 |
(sb - scb)*(sb - scb); |
| 979 |
#endif |
| 980 |
|
| 981 |
if( di <= dr && di <= dg && di <= db ) { |
| 982 |
|
| 983 |
raw_rowbuf[col] = i; |
| 984 |
upr = ucr; upg = ucg; upb = ucb; |
| 985 |
spr = scr; spg = scg; spb = scb; |
| 986 |
} |
| 987 |
else |
| 988 |
if( db <= dr && db <= dg ) { |
| 989 |
raw_rowbuf[col] = sb + hamcode_blue; |
| 990 |
spb = sb; |
| 991 |
upb = ub; |
| 992 |
} |
| 993 |
else |
| 994 |
if( dr <= dg ) { |
| 995 |
raw_rowbuf[col] = sr + hamcode_red; |
| 996 |
spr = sr; |
| 997 |
upr = ur; |
| 998 |
} |
| 999 |
else { |
| 1000 |
raw_rowbuf[col] = sg + hamcode_green; |
| 1001 |
spg = sg; |
| 1002 |
upg = ug; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
else { |
| 1007 |
raw_rowbuf[col] = i; |
| 1008 |
upr = PPM_GETR(colormap[i]); |
| 1009 |
upg = PPM_GETG(colormap[i]); |
| 1010 |
upb = PPM_GETB(colormap[i]); |
| 1011 |
spr = itoh[upr]; |
| 1012 |
spg = itoh[upg]; |
| 1013 |
spb = itoh[upb]; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
bodysize += encodeRow(ofP, raw_rowbuf, cols, nPlanes); |
| 1017 |
if( maskmethod == mskHasMask ) |
| 1018 |
bodysize += encodeMaskrow(ofP, raw_rowbuf, cols); |
| 1019 |
} |
| 1020 |
if( ofP && ODD(bodysize) ) |
| 1021 |
putByte(0); |
| 1022 |
|
| 1023 |
free(itoh); |
| 1024 |
|
| 1025 |
|
| 1026 |
free(raw_rowbuf); |
| 1027 |
|
| 1028 |
return bodysize; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
static void |
| 1034 |
ppmToHam(FILE * const ifP, |
| 1035 |
int const cols, |
| 1036 |
int const rows, |
| 1037 |
int const maxval, |
| 1038 |
pixel * const colormapArg, |
| 1039 |
int const colorsArg, |
| 1040 |
int const cmapmaxvalArg, |
| 1041 |
int const hamplanes) { |
| 1042 |
|
| 1043 |
int hamcolors, nPlanes, i, hammaxval; |
| 1044 |
long int oldsize, bodysize, formsize, cmapsize; |
| 1045 |
int * table; |
| 1046 |
int colors; |
| 1047 |
pixel * colormap; |
| 1048 |
int cmapmaxval; |
| 1049 |
|
| 1050 |
table = NULL; |
| 1051 |
colors = colorsArg; |
| 1052 |
colormap = colormapArg; |
| 1053 |
cmapmaxval = cmapmaxvalArg; |
| 1054 |
|
| 1055 |
if( maskmethod == mskHasTransparentColor ) { |
| 1056 |
pm_message("masking method '%s' not usable with HAM - " |
| 1057 |
"using '%s' instead", |
| 1058 |
mskNAME[mskHasTransparentColor], mskNAME[mskHasMask]); |
| 1059 |
maskmethod = mskHasMask; |
| 1060 |
} |
| 1061 |
|
| 1062 |
hamcolors = 1 << (hamplanes-2); |
| 1063 |
hammaxval = pm_bitstomaxval(hamplanes-2); |
| 1064 |
|
| 1065 |
if( colors == 0 ) { |
| 1066 |
|
| 1067 |
switch( hammapmode ) { |
| 1068 |
case HAMMODE_GRAY: |
| 1069 |
colors = hamcolors; |
| 1070 |
MALLOCARRAY_NOFAIL(colormap, colors); |
| 1071 |
table = makeValTable(hammaxval, MAXCOLVAL); |
| 1072 |
for( i = 0; i < colors; i++ ) |
| 1073 |
PPM_ASSIGN(colormap[i], table[i], table[i], table[i]); |
| 1074 |
free(table); |
| 1075 |
cmapmaxval = MAXCOLVAL; |
| 1076 |
break; |
| 1077 |
case HAMMODE_FIXED: { |
| 1078 |
int entries, val; |
| 1079 |
double step; |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
entries = (hamcolors-1)/7; |
| 1087 |
colors = 7*entries+1; |
| 1088 |
MALLOCARRAY_NOFAIL(colormap, colors); |
| 1089 |
step = (double)MAXCOLVAL / (double)entries; |
| 1090 |
|
| 1091 |
PPM_ASSIGN(colormap[0], 0, 0, 0); |
| 1092 |
for( i = 1; i <= entries; i++ ) { |
| 1093 |
val = (int)((double)i * step); |
| 1094 |
PPM_ASSIGN(colormap[ i], val, 0, 0); |
| 1095 |
PPM_ASSIGN(colormap[ entries+i], 0, val, 0); |
| 1096 |
PPM_ASSIGN(colormap[2*entries+i], 0, 0, val); |
| 1097 |
PPM_ASSIGN(colormap[3*entries+i], val, val, 0); |
| 1098 |
PPM_ASSIGN(colormap[4*entries+i], val, 0, val); |
| 1099 |
PPM_ASSIGN(colormap[5*entries+i], 0, val, val); |
| 1100 |
PPM_ASSIGN(colormap[6*entries+i], val, val, val); |
| 1101 |
} |
| 1102 |
cmapmaxval = MAXCOLVAL; |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
case HAMMODE_RGB4: |
| 1106 |
colormap = computeHamCmap(cols, rows, maxval, hamcolors, |
| 1107 |
&colors, 4); |
| 1108 |
cmapmaxval = 15; |
| 1109 |
break; |
| 1110 |
case HAMMODE_RGB5: |
| 1111 |
colormap = computeHamCmap(cols, rows, maxval, |
| 1112 |
hamcolors, &colors, 5); |
| 1113 |
cmapmaxval = 31; |
| 1114 |
break; |
| 1115 |
default: |
| 1116 |
pm_error("ppm_to_ham(): unknown hammapmode - can't happen"); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
else { |
| 1120 |
hammapmode = HAMMODE_MAPFILE; |
| 1121 |
if( colors > hamcolors ) { |
| 1122 |
pm_message("colormap too large - using first %d colors", |
| 1123 |
hamcolors); |
| 1124 |
colors = hamcolors; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
if( cmapmaxval != maxval ) { |
| 1129 |
int i, *table; |
| 1130 |
pixel *newcmap; |
| 1131 |
|
| 1132 |
newcmap = ppm_allocrow(colors); |
| 1133 |
table = makeValTable(cmapmaxval, maxval); |
| 1134 |
for( i = 0; i < colors; i++ ) |
| 1135 |
PPM_ASSIGN(newcmap[i], |
| 1136 |
table[PPM_GETR(colormap[i])], |
| 1137 |
table[PPM_GETG(colormap[i])], |
| 1138 |
table[PPM_GETB(colormap[i])]); |
| 1139 |
free(table); |
| 1140 |
ppm_freerow(colormap); |
| 1141 |
colormap = newcmap; |
| 1142 |
} |
| 1143 |
if( sortcmap ) |
| 1144 |
ppm_sortcolorrow(colormap, colors, PPM_STDSORT); |
| 1145 |
|
| 1146 |
nPlanes = hamplanes; |
| 1147 |
cmapsize = colors * 3; |
| 1148 |
|
| 1149 |
bodysize = oldsize = rows * TOTALPLANES(nPlanes) * ilbm_rowByteCt(cols); |
| 1150 |
if( DO_COMPRESS ) { |
| 1151 |
bodysize = doHamBody(ifP, NULL, cols, rows, maxval, |
| 1152 |
hammaxval, nPlanes, colormap, colors); |
| 1153 |
|
| 1154 |
|
| 1155 |
if( bodysize > oldsize ) |
| 1156 |
pm_message("warning - %s compression increases BODY size " |
| 1157 |
"by %ld%%", |
| 1158 |
cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); |
| 1159 |
else |
| 1160 |
pm_message("BODY compression (%s): %ld%%", |
| 1161 |
cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
formsize = |
| 1166 |
4 + |
| 1167 |
4 + 4 + BitMapHeaderSize + |
| 1168 |
4 + 4 + CAMGChunkSize + |
| 1169 |
4 + 4 + cmapsize + PAD(cmapsize) + |
| 1170 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1171 |
lengthOfTextChunks(); |
| 1172 |
|
| 1173 |
pm_writebiglong(stdout, ID_FORM); |
| 1174 |
pm_writebiglong(stdout, formsize); |
| 1175 |
pm_writebiglong(stdout, ID_ILBM); |
| 1176 |
|
| 1177 |
writeBmhd(cols, rows, nPlanes); |
| 1178 |
writeTextChunks(); |
| 1179 |
writeCamg(); |
| 1180 |
writeCmap(colormap, colors, maxval); |
| 1181 |
|
| 1182 |
|
| 1183 |
pm_writebiglong(stdout, ID_BODY); |
| 1184 |
pm_writebiglong(stdout, bodysize); |
| 1185 |
if( DO_COMPRESS ) |
| 1186 |
writeBodyRows(); |
| 1187 |
else |
| 1188 |
doHamBody(ifP, stdout, cols, rows, maxval, hammaxval, |
| 1189 |
nPlanes, colormap, colors); |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
static long int |
| 1199 |
doDeepBody(FILE * const ifP, |
| 1200 |
FILE * const ofP, |
| 1201 |
int const cols, |
| 1202 |
int const rows, |
| 1203 |
pixval const maxval, |
| 1204 |
int const bitspercolor) { |
| 1205 |
|
| 1206 |
int row, col; |
| 1207 |
pixel *pP; |
| 1208 |
int *table = NULL; |
| 1209 |
long int bodysize = 0; |
| 1210 |
rawtype *redbuf, *greenbuf, *bluebuf; |
| 1211 |
int newmaxval; |
| 1212 |
|
| 1213 |
MALLOCARRAY_NOFAIL(redbuf, cols); |
| 1214 |
MALLOCARRAY_NOFAIL(greenbuf, cols); |
| 1215 |
MALLOCARRAY_NOFAIL(bluebuf, cols); |
| 1216 |
|
| 1217 |
newmaxval = pm_bitstomaxval(bitspercolor); |
| 1218 |
if( maxval != newmaxval ) { |
| 1219 |
pm_message("maxval is not %d - automatically rescaling colors", |
| 1220 |
newmaxval); |
| 1221 |
table = makeValTable(maxval, newmaxval); |
| 1222 |
} |
| 1223 |
|
| 1224 |
for( row = 0; row < rows; row++ ) { |
| 1225 |
pP = nextPixrow(ifP, row); |
| 1226 |
if( table ) { |
| 1227 |
for( col = 0; col < cols; col++, pP++ ) { |
| 1228 |
redbuf[col] = table[PPM_GETR(*pP)]; |
| 1229 |
greenbuf[col] = table[PPM_GETG(*pP)]; |
| 1230 |
bluebuf[col] = table[PPM_GETB(*pP)]; |
| 1231 |
} |
| 1232 |
} |
| 1233 |
else { |
| 1234 |
for( col = 0; col < cols; col++, pP++ ) { |
| 1235 |
redbuf[col] = PPM_GETR(*pP); |
| 1236 |
greenbuf[col] = PPM_GETG(*pP); |
| 1237 |
bluebuf[col] = PPM_GETB(*pP); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
bodysize += encodeRow(ofP, redbuf, cols, bitspercolor); |
| 1241 |
bodysize += encodeRow(ofP, greenbuf, cols, bitspercolor); |
| 1242 |
bodysize += encodeRow(ofP, bluebuf, cols, bitspercolor); |
| 1243 |
if( maskmethod == mskHasMask ) |
| 1244 |
bodysize += encodeMaskrow(ofP, redbuf, cols); |
| 1245 |
} |
| 1246 |
if( ofP && ODD(bodysize) ) |
| 1247 |
putByte(0); |
| 1248 |
|
| 1249 |
|
| 1250 |
if( table ) |
| 1251 |
free(table); |
| 1252 |
free(redbuf); |
| 1253 |
free(greenbuf); |
| 1254 |
free(bluebuf); |
| 1255 |
|
| 1256 |
return bodysize; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
static void |
| 1262 |
ppmToDeep(FILE * const ifP, |
| 1263 |
int const cols, |
| 1264 |
int const rows, |
| 1265 |
int const maxval, |
| 1266 |
int const bitspercolor) { |
| 1267 |
|
| 1268 |
int nPlanes; |
| 1269 |
long int bodysize, oldsize, formsize; |
| 1270 |
|
| 1271 |
if( maskmethod == mskHasTransparentColor ) { |
| 1272 |
pm_message("masking method '%s' not usable with deep ILBM - " |
| 1273 |
"using '%s' instead", |
| 1274 |
mskNAME[mskHasTransparentColor], mskNAME[mskHasMask]); |
| 1275 |
maskmethod = mskHasMask; |
| 1276 |
} |
| 1277 |
|
| 1278 |
nPlanes = 3*bitspercolor; |
| 1279 |
|
| 1280 |
bodysize = oldsize = rows * TOTALPLANES(nPlanes) * ilbm_rowByteCt(cols); |
| 1281 |
if( DO_COMPRESS ) { |
| 1282 |
bodysize = doDeepBody(ifP, NULL, cols, rows, maxval, bitspercolor); |
| 1283 |
if( bodysize > oldsize ) |
| 1284 |
pm_message("warning - %s compression increases BODY size by %ld%%", |
| 1285 |
cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); |
| 1286 |
else |
| 1287 |
pm_message("BODY compression (%s): %ld%%", |
| 1288 |
cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); |
| 1289 |
} |
| 1290 |
|
| 1291 |
formsize = |
| 1292 |
4 + |
| 1293 |
4 + 4 + BitMapHeaderSize + |
| 1294 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1295 |
lengthOfTextChunks(); |
| 1296 |
if( gen_camg ) |
| 1297 |
formsize += 4 + 4 + CAMGChunkSize; |
| 1298 |
|
| 1299 |
pm_writebiglong(stdout, ID_FORM); |
| 1300 |
pm_writebiglong(stdout, formsize); |
| 1301 |
pm_writebiglong(stdout, ID_ILBM); |
| 1302 |
|
| 1303 |
writeBmhd(cols, rows, nPlanes); |
| 1304 |
writeTextChunks(); |
| 1305 |
if( gen_camg ) |
| 1306 |
writeCamg(); |
| 1307 |
|
| 1308 |
|
| 1309 |
pm_writebiglong(stdout, ID_BODY); |
| 1310 |
pm_writebiglong(stdout, bodysize); |
| 1311 |
if( DO_COMPRESS ) |
| 1312 |
writeBodyRows(); |
| 1313 |
else |
| 1314 |
doDeepBody(ifP, stdout, cols, rows, maxval, bitspercolor); |
| 1315 |
} |
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
static long int |
| 1324 |
doDcolBody(FILE * const ifP, |
| 1325 |
FILE * const ofP, |
| 1326 |
int const cols, |
| 1327 |
int const rows, |
| 1328 |
pixval const maxval, |
| 1329 |
DirectColor * const dcol) { |
| 1330 |
|
| 1331 |
int row, col; |
| 1332 |
pixel *pP; |
| 1333 |
long int bodysize = 0; |
| 1334 |
rawtype *redbuf, *greenbuf, *bluebuf; |
| 1335 |
int *redtable, *greentable, *bluetable; |
| 1336 |
|
| 1337 |
MALLOCARRAY_NOFAIL(redbuf, cols); |
| 1338 |
MALLOCARRAY_NOFAIL(greenbuf, cols); |
| 1339 |
MALLOCARRAY_NOFAIL(bluebuf, cols); |
| 1340 |
|
| 1341 |
redtable = makeValTable(maxval, pm_bitstomaxval(dcol->r)); |
| 1342 |
greentable = makeValTable(maxval, pm_bitstomaxval(dcol->g)); |
| 1343 |
bluetable = makeValTable(maxval, pm_bitstomaxval(dcol->b)); |
| 1344 |
|
| 1345 |
for( row = 0; row < rows; row++ ) { |
| 1346 |
pP = nextPixrow(ifP, row); |
| 1347 |
for( col = 0; col < cols; col++, pP++ ) { |
| 1348 |
redbuf[col] = redtable[PPM_GETR(*pP)]; |
| 1349 |
greenbuf[col] = greentable[PPM_GETG(*pP)]; |
| 1350 |
bluebuf[col] = bluetable[PPM_GETB(*pP)]; |
| 1351 |
} |
| 1352 |
bodysize += encodeRow(ofP, redbuf, cols, dcol->r); |
| 1353 |
bodysize += encodeRow(ofP, greenbuf, cols, dcol->g); |
| 1354 |
bodysize += encodeRow(ofP, bluebuf, cols, dcol->b); |
| 1355 |
if( maskmethod == mskHasMask ) |
| 1356 |
bodysize += encodeMaskrow(ofP, redbuf, cols); |
| 1357 |
} |
| 1358 |
if( ofP && ODD(bodysize) ) |
| 1359 |
putByte(0); |
| 1360 |
|
| 1361 |
|
| 1362 |
free(redtable); |
| 1363 |
free(greentable); |
| 1364 |
free(bluetable); |
| 1365 |
free(redbuf); |
| 1366 |
free(greenbuf); |
| 1367 |
free(bluebuf); |
| 1368 |
|
| 1369 |
return bodysize; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
static void |
| 1375 |
ppmToDcol(FILE * const ifP, |
| 1376 |
int const cols, |
| 1377 |
int const rows, |
| 1378 |
int const maxval, |
| 1379 |
DirectColor * const dcol) { |
| 1380 |
|
| 1381 |
int nPlanes; |
| 1382 |
long int bodysize, oldsize, formsize; |
| 1383 |
|
| 1384 |
if( maskmethod == mskHasTransparentColor ) { |
| 1385 |
pm_message("masking method '%s' not usable with deep ILBM - " |
| 1386 |
"using '%s' instead", |
| 1387 |
mskNAME[mskHasTransparentColor], mskNAME[mskHasMask]); |
| 1388 |
maskmethod = mskHasMask; |
| 1389 |
} |
| 1390 |
|
| 1391 |
nPlanes = dcol->r + dcol->g + dcol->b; |
| 1392 |
|
| 1393 |
bodysize = oldsize = rows * TOTALPLANES(nPlanes) * ilbm_rowByteCt(cols); |
| 1394 |
if( DO_COMPRESS ) { |
| 1395 |
bodysize = doDcolBody(ifP, NULL, cols, rows, maxval, dcol); |
| 1396 |
if( bodysize > oldsize ) |
| 1397 |
pm_message("warning - %s compression increases BODY size by %ld%%", |
| 1398 |
cmpNAME[compmethod], |
| 1399 |
100*(bodysize-oldsize)/oldsize); |
| 1400 |
else |
| 1401 |
pm_message("BODY compression (%s): %ld%%", cmpNAME[compmethod], |
| 1402 |
100*(oldsize-bodysize)/oldsize); |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
formsize = |
| 1407 |
4 + |
| 1408 |
4 + 4 + BitMapHeaderSize + |
| 1409 |
4 + 4 + DirectColorSize + |
| 1410 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1411 |
lengthOfTextChunks(); |
| 1412 |
if( gen_camg ) |
| 1413 |
formsize += 4 + 4 + CAMGChunkSize; |
| 1414 |
|
| 1415 |
pm_writebiglong(stdout, ID_FORM); |
| 1416 |
pm_writebiglong(stdout, formsize); |
| 1417 |
pm_writebiglong(stdout, ID_ILBM); |
| 1418 |
|
| 1419 |
writeBmhd(cols, rows, nPlanes); |
| 1420 |
writeTextChunks(); |
| 1421 |
|
| 1422 |
pm_writebiglong(stdout, ID_DCOL); |
| 1423 |
pm_writebiglong(stdout, DirectColorSize); |
| 1424 |
putByte(dcol->r); |
| 1425 |
putByte(dcol->g); |
| 1426 |
putByte(dcol->b); |
| 1427 |
putByte(0); |
| 1428 |
|
| 1429 |
if( gen_camg ) |
| 1430 |
writeCamg(); |
| 1431 |
|
| 1432 |
|
| 1433 |
pm_writebiglong(stdout, ID_BODY); |
| 1434 |
pm_writebiglong(stdout, bodysize); |
| 1435 |
if( DO_COMPRESS ) |
| 1436 |
writeBodyRows(); |
| 1437 |
else |
| 1438 |
doDcolBody(ifP, stdout, cols, rows, maxval, dcol); |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
static long int |
| 1448 |
doStdBody(FILE * const ifP, |
| 1449 |
FILE * const ofP, |
| 1450 |
int const cols, |
| 1451 |
int const rows, |
| 1452 |
pixval const maxval, |
| 1453 |
pixel * const colormap, |
| 1454 |
int const colors, |
| 1455 |
int const nPlanes) { |
| 1456 |
|
| 1457 |
int row, col, i; |
| 1458 |
pixel *pP; |
| 1459 |
rawtype *raw_rowbuf; |
| 1460 |
long int bodysize = 0; |
| 1461 |
int usehash = 1; |
| 1462 |
colorhash_table cht; |
| 1463 |
|
| 1464 |
MALLOCARRAY_NOFAIL(raw_rowbuf, cols); |
| 1465 |
cht = ppm_colorrowtocolorhash(colormap, colors); |
| 1466 |
|
| 1467 |
for( row = 0; row < rows; row++ ) { |
| 1468 |
pixel *prow; |
| 1469 |
prow = nextPixrow(ifP, row); |
| 1470 |
|
| 1471 |
for( col = 0; col < cols; ++col ) { |
| 1472 |
pP = &prow[col]; |
| 1473 |
|
| 1474 |
if( maskmethod == mskHasTransparentColor && |
| 1475 |
maskrow[col] == PBM_WHITE ) |
| 1476 |
i = transpIndex; |
| 1477 |
else { |
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
i = ppm_lookupcolor(cht, pP); |
| 1482 |
if( i == -1 ) { |
| 1483 |
i = ppm_findclosestcolor(colormap, colors, pP); |
| 1484 |
|
| 1485 |
if( usehash ) { |
| 1486 |
if( ppm_addtocolorhash(cht, pP, i) < 0 ) { |
| 1487 |
pm_message("out of memory adding to hash table, " |
| 1488 |
"proceeding without it"); |
| 1489 |
usehash = 0; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
raw_rowbuf[col] = i; |
| 1495 |
} |
| 1496 |
bodysize += encodeRow(ofP, raw_rowbuf, cols, nPlanes); |
| 1497 |
if( maskmethod == mskHasMask ) |
| 1498 |
bodysize += encodeMaskrow(ofP, raw_rowbuf, cols); |
| 1499 |
} |
| 1500 |
if( ofP && ODD(bodysize) ) |
| 1501 |
putByte(0); |
| 1502 |
|
| 1503 |
|
| 1504 |
ppm_freecolorhash(cht); |
| 1505 |
free(raw_rowbuf); |
| 1506 |
|
| 1507 |
return bodysize; |
| 1508 |
} |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
static void |
| 1513 |
ppmToStd(FILE * const ifP, |
| 1514 |
int const cols, |
| 1515 |
int const rows, |
| 1516 |
int const maxval, |
| 1517 |
pixel * const colormapArg, |
| 1518 |
int const colorsArg, |
| 1519 |
int const cmapmaxvalArg, |
| 1520 |
int const maxcolors, |
| 1521 |
int const nPlanes) { |
| 1522 |
|
| 1523 |
long int formsize, cmapsize, bodysize, oldsize; |
| 1524 |
|
| 1525 |
int colors; |
| 1526 |
pixel * colormap; |
| 1527 |
int cmapmaxval; |
| 1528 |
|
| 1529 |
colors = colorsArg; |
| 1530 |
colormap = colormapArg; |
| 1531 |
cmapmaxval = cmapmaxvalArg; |
| 1532 |
|
| 1533 |
if( maskmethod == mskHasTransparentColor ) { |
| 1534 |
if( transpColor ) { |
| 1535 |
transpIndex = |
| 1536 |
ppm_addtocolorrow(colormap, &colors, maxcolors, transpColor); |
| 1537 |
} |
| 1538 |
else |
| 1539 |
if( colors < maxcolors ) |
| 1540 |
transpIndex = colors; |
| 1541 |
|
| 1542 |
if( transpIndex < 0 ) { |
| 1543 |
pm_message("too many colors for masking method '%s' - " |
| 1544 |
"using '%s' instead", |
| 1545 |
mskNAME[mskHasTransparentColor], mskNAME[mskHasMask]); |
| 1546 |
maskmethod = mskHasMask; |
| 1547 |
} |
| 1548 |
} |
| 1549 |
|
| 1550 |
if( cmapmaxval != maxval ) { |
| 1551 |
int i, *table; |
| 1552 |
pixel *newcmap; |
| 1553 |
|
| 1554 |
newcmap = ppm_allocrow(colors); |
| 1555 |
table = makeValTable(cmapmaxval, maxval); |
| 1556 |
for (i = 0; i < colors; ++i) |
| 1557 |
PPM_ASSIGN(newcmap[i], |
| 1558 |
table[PPM_GETR(colormap[i])], |
| 1559 |
table[PPM_GETG(colormap[i])], |
| 1560 |
table[PPM_GETB(colormap[i])]); |
| 1561 |
free(table); |
| 1562 |
colormap = newcmap; |
| 1563 |
} |
| 1564 |
if( sortcmap ) |
| 1565 |
ppm_sortcolorrow(colormap, colors, PPM_STDSORT); |
| 1566 |
|
| 1567 |
bodysize = oldsize = rows * TOTALPLANES(nPlanes) * ilbm_rowByteCt(cols); |
| 1568 |
if( DO_COMPRESS ) { |
| 1569 |
bodysize = doStdBody(ifP, NULL, cols, rows, maxval, colormap, |
| 1570 |
colors, nPlanes); |
| 1571 |
if( bodysize > oldsize ) |
| 1572 |
pm_message("warning - %s compression increases BODY size by %ld%%", |
| 1573 |
cmpNAME[compmethod], 100*(bodysize-oldsize)/oldsize); |
| 1574 |
else |
| 1575 |
pm_message("BODY compression (%s): %ld%%", |
| 1576 |
cmpNAME[compmethod], 100*(oldsize-bodysize)/oldsize); |
| 1577 |
} |
| 1578 |
|
| 1579 |
cmapsize = colors * 3; |
| 1580 |
|
| 1581 |
formsize = |
| 1582 |
4 + |
| 1583 |
4 + 4 + BitMapHeaderSize + |
| 1584 |
4 + 4 + cmapsize + PAD(cmapsize) + |
| 1585 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1586 |
lengthOfTextChunks(); |
| 1587 |
if( gen_camg ) |
| 1588 |
formsize += 4 + 4 + CAMGChunkSize; |
| 1589 |
|
| 1590 |
pm_writebiglong(stdout, ID_FORM); |
| 1591 |
pm_writebiglong(stdout, formsize); |
| 1592 |
pm_writebiglong(stdout, ID_ILBM); |
| 1593 |
|
| 1594 |
writeBmhd(cols, rows, nPlanes); |
| 1595 |
writeTextChunks(); |
| 1596 |
if( gen_camg ) |
| 1597 |
writeCamg(); |
| 1598 |
writeCmap(colormap, colors, maxval); |
| 1599 |
|
| 1600 |
|
| 1601 |
pm_writebiglong(stdout, ID_BODY); |
| 1602 |
pm_writebiglong(stdout, bodysize); |
| 1603 |
if( DO_COMPRESS ) |
| 1604 |
writeBodyRows(); |
| 1605 |
else |
| 1606 |
doStdBody(ifP, stdout, cols, rows, maxval, colormap, colors, nPlanes); |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
static void |
| 1616 |
ppmToRgb8(FILE * const ifP, |
| 1617 |
int const cols, |
| 1618 |
int const rows, |
| 1619 |
int const maxval) { |
| 1620 |
|
| 1621 |
long int bodysize, oldsize, formsize; |
| 1622 |
pixel *pP; |
| 1623 |
int *table = NULL; |
| 1624 |
int row, col1, col2, compr_len, len; |
| 1625 |
unsigned char *compr_row; |
| 1626 |
|
| 1627 |
maskmethod = 0; |
| 1628 |
compmethod = 4; |
| 1629 |
MALLOCARRAY_NOFAIL(compr_row, cols * 4); |
| 1630 |
|
| 1631 |
if( maxval != 255 ) { |
| 1632 |
pm_message("maxval is not 255 - automatically rescaling colors"); |
| 1633 |
table = makeValTable(maxval, 255); |
| 1634 |
} |
| 1635 |
|
| 1636 |
oldsize = cols * rows * 4; |
| 1637 |
bodysize = 0; |
| 1638 |
for( row = 0; row < rows; row++ ) { |
| 1639 |
pP = nextPixrow(ifP, row); |
| 1640 |
compr_len = 0; |
| 1641 |
for( col1 = 0; col1 < cols; col1 = col2 ) { |
| 1642 |
col2 = col1 + 1; |
| 1643 |
if( maskrow ) { |
| 1644 |
while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && |
| 1645 |
maskrow[col1] == maskrow[col2] ) |
| 1646 |
col2++; |
| 1647 |
} |
| 1648 |
else { |
| 1649 |
while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) ) |
| 1650 |
col2++; |
| 1651 |
} |
| 1652 |
len = col2 - col1; |
| 1653 |
while( len ) { |
| 1654 |
int count; |
| 1655 |
count = (len > 127 ? 127 : len); |
| 1656 |
len -= count; |
| 1657 |
if( table ) { |
| 1658 |
compr_row[compr_len++] = table[PPM_GETR(pP[col1])]; |
| 1659 |
compr_row[compr_len++] = table[PPM_GETG(pP[col1])]; |
| 1660 |
compr_row[compr_len++] = table[PPM_GETB(pP[col1])]; |
| 1661 |
} |
| 1662 |
else { |
| 1663 |
compr_row[compr_len++] = PPM_GETR(pP[col1]); |
| 1664 |
compr_row[compr_len++] = PPM_GETG(pP[col1]); |
| 1665 |
compr_row[compr_len++] = PPM_GETB(pP[col1]); |
| 1666 |
} |
| 1667 |
compr_row[compr_len] = count; |
| 1668 |
if( maskrow && maskrow[col1] == PBM_WHITE ) |
| 1669 |
compr_row[compr_len] |= 1<<7; |
| 1670 |
++compr_len; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
storeBodyrow(compr_row, compr_len); |
| 1674 |
bodysize += compr_len; |
| 1675 |
} |
| 1676 |
|
| 1677 |
pm_message("BODY compression: %ld%%", 100*(oldsize-bodysize)/oldsize); |
| 1678 |
|
| 1679 |
formsize = |
| 1680 |
4 + |
| 1681 |
4 + 4 + BitMapHeaderSize + |
| 1682 |
4 + 4 + CAMGChunkSize + |
| 1683 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1684 |
lengthOfTextChunks(); |
| 1685 |
|
| 1686 |
|
| 1687 |
pm_writebiglong(stdout, ID_FORM); |
| 1688 |
pm_writebiglong(stdout, formsize); |
| 1689 |
pm_writebiglong(stdout, ID_RGB8); |
| 1690 |
|
| 1691 |
writeBmhd(cols, rows, 25); |
| 1692 |
writeTextChunks(); |
| 1693 |
writeCamg(); |
| 1694 |
|
| 1695 |
pm_writebiglong(stdout, ID_BODY); |
| 1696 |
pm_writebiglong(stdout, bodysize); |
| 1697 |
writeBodyRows(); |
| 1698 |
} |
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
static void |
| 1707 |
ppmToRgbn(FILE * const ifP, |
| 1708 |
int const cols, |
| 1709 |
int const rows, |
| 1710 |
int const maxval) { |
| 1711 |
|
| 1712 |
long int bodysize, oldsize, formsize; |
| 1713 |
pixel *pP; |
| 1714 |
int *table = NULL; |
| 1715 |
int row, col1, col2, compr_len, len; |
| 1716 |
unsigned char *compr_row; |
| 1717 |
|
| 1718 |
maskmethod = 0; |
| 1719 |
compmethod = 4; |
| 1720 |
MALLOCARRAY_NOFAIL(compr_row, cols * 2); |
| 1721 |
|
| 1722 |
if( maxval != 15 ) { |
| 1723 |
pm_message("maxval is not 15 - automatically rescaling colors"); |
| 1724 |
table = makeValTable(maxval, 15); |
| 1725 |
} |
| 1726 |
|
| 1727 |
oldsize = cols * rows * 2; |
| 1728 |
bodysize = 0; |
| 1729 |
for( row = 0; row < rows; row++ ) { |
| 1730 |
pP = nextPixrow(ifP, row); |
| 1731 |
compr_len = 0; |
| 1732 |
for( col1 = 0; col1 < cols; col1 = col2 ) { |
| 1733 |
col2 = col1 + 1; |
| 1734 |
if( maskrow ) { |
| 1735 |
while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) && |
| 1736 |
maskrow[col1] == maskrow[col2] ) |
| 1737 |
col2++; |
| 1738 |
} |
| 1739 |
else { |
| 1740 |
while( col2 < cols && PPM_EQUAL(pP[col1], pP[col2]) ) |
| 1741 |
col2++; |
| 1742 |
} |
| 1743 |
len = col2 - col1; |
| 1744 |
while( len ) { |
| 1745 |
int count; |
| 1746 |
count = (len > 65535 ? 65535 : len); |
| 1747 |
len -= count; |
| 1748 |
if( table ) { |
| 1749 |
compr_row[compr_len] = table[PPM_GETR(pP[col1])] << 4; |
| 1750 |
compr_row[compr_len] |= table[PPM_GETG(pP[col1])]; |
| 1751 |
++compr_len; |
| 1752 |
compr_row[compr_len] = table[PPM_GETB(pP[col1])] << 4; |
| 1753 |
} |
| 1754 |
else { |
| 1755 |
compr_row[compr_len] = PPM_GETR(pP[col1]) << 4; |
| 1756 |
compr_row[compr_len] |= PPM_GETG(pP[col1]); |
| 1757 |
++compr_len; |
| 1758 |
compr_row[compr_len] = PPM_GETB(pP[col1]) << 4; |
| 1759 |
} |
| 1760 |
if( maskrow && maskrow[col1] == PBM_WHITE ) |
| 1761 |
compr_row[compr_len] |= 1<<3; |
| 1762 |
if( count <= 7 ) |
| 1763 |
compr_row[compr_len++] |= count; |
| 1764 |
else { |
| 1765 |
++compr_len; |
| 1766 |
if( count <= 255 ) |
| 1767 |
compr_row[compr_len++] = (unsigned char)count; |
| 1768 |
|
| 1769 |
else { |
| 1770 |
compr_row[compr_len++] = (unsigned char)0; |
| 1771 |
|
| 1772 |
compr_row[compr_len++] = (count >> 8) & 0xff; |
| 1773 |
|
| 1774 |
compr_row[compr_len++] = count & 0xff; |
| 1775 |
|
| 1776 |
} |
| 1777 |
} |
| 1778 |
} |
| 1779 |
} |
| 1780 |
storeBodyrow(compr_row, compr_len); |
| 1781 |
bodysize += compr_len; |
| 1782 |
} |
| 1783 |
|
| 1784 |
pm_message("BODY compression: %ld%%", 100*(oldsize-bodysize)/oldsize); |
| 1785 |
|
| 1786 |
formsize = |
| 1787 |
4 + |
| 1788 |
4 + 4 + BitMapHeaderSize + |
| 1789 |
4 + 4 + CAMGChunkSize + |
| 1790 |
4 + 4 + bodysize + PAD(bodysize) + |
| 1791 |
lengthOfTextChunks(); |
| 1792 |
|
| 1793 |
|
| 1794 |
pm_writebiglong(stdout, ID_FORM); |
| 1795 |
pm_writebiglong(stdout, formsize); |
| 1796 |
pm_writebiglong(stdout, ID_RGBN); |
| 1797 |
|
| 1798 |
writeBmhd(cols, rows, 13); |
| 1799 |
writeTextChunks(); |
| 1800 |
writeCamg(); |
| 1801 |
|
| 1802 |
pm_writebiglong(stdout, ID_BODY); |
| 1803 |
pm_writebiglong(stdout, bodysize); |
| 1804 |
writeBodyRows(); |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
#ifdef ILBM_PCHG |
| 1814 |
static pixel *ppmslice[2]; |
| 1815 |
|
| 1816 |
void |
| 1817 |
ppmToPchg() { |
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
} |
| 1843 |
#endif |
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
int |
| 1848 |
main(int argc, char ** argv) { |
| 1849 |
|
| 1850 |
FILE * ifP; |
| 1851 |
int argn, rows, cols, format, nPlanes; |
| 1852 |
int ifmode, forcemode, maxplanes, fixplanes, mode; |
| 1853 |
int hamplanes; |
| 1854 |
int deepbits; |
| 1855 |
DirectColor dcol; |
| 1856 |
#define MAXCOLORS (1<<maxplanes) |
| 1857 |
pixval maxval; |
| 1858 |
pixel * colormap; |
| 1859 |
int colors = 0; |
| 1860 |
pixval cmapmaxval; |
| 1861 |
const char * mapfile; |
| 1862 |
const char * transpname; |
| 1863 |
unsigned int defunctArgs = 0; |
| 1864 |
|
| 1865 |
ppm_init(&argc, argv); |
| 1866 |
|
| 1867 |
colormap = NULL; |
| 1868 |
ifmode = DEF_IFMODE; forcemode = MODE_NONE; |
| 1869 |
maxplanes = DEF_MAXPLANES; fixplanes = 0; |
| 1870 |
hamplanes = DEF_HAMPLANES; |
| 1871 |
deepbits = DEF_DEEPPLANES; |
| 1872 |
dcol.r = dcol.g = dcol.b = DEF_DCOLPLANES; |
| 1873 |
mapfile = transpname = NULL; |
| 1874 |
|
| 1875 |
argn = 1; |
| 1876 |
while( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { |
| 1877 |
if( pm_keymatch(argv[argn], "-ilbm", 5) ) { |
| 1878 |
if( forcemode == MODE_RGB8 || forcemode == MODE_RGBN ) |
| 1879 |
forcemode = MODE_NONE; |
| 1880 |
} |
| 1881 |
else |
| 1882 |
if( pm_keymatch(argv[argn], "-rgb8", 5) ) |
| 1883 |
forcemode = MODE_RGB8; |
| 1884 |
else |
| 1885 |
if( pm_keymatch(argv[argn], "-rgbn", 5) ) |
| 1886 |
forcemode = MODE_RGBN; |
| 1887 |
else |
| 1888 |
if( pm_keymatch(argv[argn], "-maxplanes", 4) || |
| 1889 |
pm_keymatch(argv[argn], "-mp", 3) ) { |
| 1890 |
if( ++argn >= argc ) |
| 1891 |
pm_error("-maxplanes requires a value"); |
| 1892 |
maxplanes = getIntVal(argv[argn], argv[argn-1], 1, MAXPLANES); |
| 1893 |
fixplanes = 0; |
| 1894 |
} |
| 1895 |
else |
| 1896 |
if( pm_keymatch(argv[argn], "-fixplanes", 4) || |
| 1897 |
pm_keymatch(argv[argn], "-fp", 3) ) { |
| 1898 |
if( ++argn >= argc ) |
| 1899 |
pm_error("-fixplanes requires a value"); |
| 1900 |
fixplanes = getIntVal(argv[argn], argv[argn-1], 1, MAXPLANES); |
| 1901 |
maxplanes = fixplanes; |
| 1902 |
} |
| 1903 |
else |
| 1904 |
if( pm_keymatch(argv[argn], "-mapfile", 4) ) { |
| 1905 |
if( ++argn >= argc ) |
| 1906 |
pm_error("-mapfile requires a value"); |
| 1907 |
mapfile = argv[argn]; |
| 1908 |
} |
| 1909 |
else |
| 1910 |
if( pm_keymatch(argv[argn], "-mmethod", 3) ) { |
| 1911 |
if( ++argn >= argc ) |
| 1912 |
pm_error("-mmethod requires a value"); |
| 1913 |
maskmethod = getMaskType(argv[argn]); |
| 1914 |
switch( maskmethod ) { |
| 1915 |
case mskNone: |
| 1916 |
case mskHasMask: |
| 1917 |
case mskHasTransparentColor: |
| 1918 |
break; |
| 1919 |
default: |
| 1920 |
pm_error("This program does not know how to handle " |
| 1921 |
"masking method '%s'", |
| 1922 |
mskNAME[maskmethod]); |
| 1923 |
} |
| 1924 |
} |
| 1925 |
else |
| 1926 |
if( pm_keymatch(argv[argn], "-maskfile", 4) ) { |
| 1927 |
if( ++argn >= argc ) |
| 1928 |
pm_error("-maskfile requires a value"); |
| 1929 |
maskfile = pm_openr(argv[argn]); |
| 1930 |
if( maskmethod == mskNone ) |
| 1931 |
maskmethod = mskHasMask; |
| 1932 |
} |
| 1933 |
else |
| 1934 |
if( pm_keymatch(argv[argn], "-transparent", 3) ) { |
| 1935 |
if( ++argn >= argc ) |
| 1936 |
pm_error("-transparent requires a value"); |
| 1937 |
transpname = argv[argn]; |
| 1938 |
if( maskmethod == mskNone ) |
| 1939 |
maskmethod = mskHasTransparentColor; |
| 1940 |
} |
| 1941 |
else |
| 1942 |
if( pm_keymatch(argv[argn], "-sortcmap", 5) ) |
| 1943 |
sortcmap = 1; |
| 1944 |
else |
| 1945 |
if( pm_keymatch(argv[argn], "-cmaponly", 3) ) { |
| 1946 |
forcemode = MODE_CMAP; |
| 1947 |
} |
| 1948 |
else |
| 1949 |
if( pm_keymatch(argv[argn], "-lace", 2) ) { |
| 1950 |
slicesize = 2; |
| 1951 |
viewportmodes |= vmLACE; |
| 1952 |
gen_camg = 1; |
| 1953 |
} |
| 1954 |
else |
| 1955 |
if( pm_keymatch(argv[argn], "-nolace", 4) ) { |
| 1956 |
slicesize = 1; |
| 1957 |
viewportmodes &= ~vmLACE; |
| 1958 |
} |
| 1959 |
else |
| 1960 |
if( pm_keymatch(argv[argn], "-hires", 3) ) { |
| 1961 |
viewportmodes |= vmHIRES; |
| 1962 |
gen_camg = 1; |
| 1963 |
} |
| 1964 |
else |
| 1965 |
if( pm_keymatch(argv[argn], "-nohires", 5) ) |
| 1966 |
viewportmodes &= ~vmHIRES; |
| 1967 |
else |
| 1968 |
if( pm_keymatch(argv[argn], "-camg", 5) ) { |
| 1969 |
char *tail; |
| 1970 |
long int value = 0L; |
| 1971 |
|
| 1972 |
if( ++argn >= argc ) |
| 1973 |
pm_error("-camg requires a value"); |
| 1974 |
value = strtol(argv[argn], &tail, 16); |
| 1975 |
if(argv[argn] == tail) |
| 1976 |
pm_error("-camg requires a value"); |
| 1977 |
viewportmodes |= value; |
| 1978 |
gen_camg = 1; |
| 1979 |
} |
| 1980 |
else |
| 1981 |
if( pm_keymatch(argv[argn], "-ecs", 2) ) { |
| 1982 |
maxplanes = ECS_MAXPLANES; |
| 1983 |
hamplanes = ECS_HAMPLANES; |
| 1984 |
} |
| 1985 |
else |
| 1986 |
if( pm_keymatch(argv[argn], "-aga", 3) ) { |
| 1987 |
maxplanes = AGA_MAXPLANES; |
| 1988 |
hamplanes = AGA_HAMPLANES; |
| 1989 |
} |
| 1990 |
else |
| 1991 |
if( pm_keymatch(argv[argn], "-hamplanes", 5) ) { |
| 1992 |
if( ++argn >= argc ) |
| 1993 |
pm_error("-hamplanes requires a value"); |
| 1994 |
hamplanes = getIntVal(argv[argn], argv[argn-1], 3, HAMMAXPLANES); |
| 1995 |
} |
| 1996 |
else |
| 1997 |
if( pm_keymatch(argv[argn], "-hambits", 5) ) { |
| 1998 |
if( ++argn >= argc ) |
| 1999 |
pm_usage("-hambits requires a value"); |
| 2000 |
hamplanes = |
| 2001 |
getIntVal(argv[argn], argv[argn-1], 3, HAMMAXPLANES-2) +2; |
| 2002 |
} |
| 2003 |
else |
| 2004 |
if( pm_keymatch(argv[argn], "-ham6", 5) ) { |
| 2005 |
hamplanes = ECS_HAMPLANES; |
| 2006 |
forcemode = MODE_HAM; |
| 2007 |
} |
| 2008 |
else |
| 2009 |
if( pm_keymatch(argv[argn], "-ham8", 5) ) { |
| 2010 |
hamplanes = AGA_HAMPLANES; |
| 2011 |
forcemode = MODE_HAM; |
| 2012 |
} |
| 2013 |
else |
| 2014 |
if( pm_keymatch(argv[argn], "-hammap", 5) ) { |
| 2015 |
if( ++argn >= argc ) |
| 2016 |
pm_error("-hammap requires a value"); |
| 2017 |
hammapmode = getHammapMode(argv[argn]); |
| 2018 |
} |
| 2019 |
else |
| 2020 |
if( pm_keymatch(argv[argn], "-hamif", 5) ) |
| 2021 |
ifmode = MODE_HAM; |
| 2022 |
else |
| 2023 |
if( pm_keymatch(argv[argn], "-nohamif", 7) ) { |
| 2024 |
if( ifmode == MODE_HAM ) |
| 2025 |
ifmode = MODE_NONE; |
| 2026 |
} |
| 2027 |
else |
| 2028 |
if( pm_keymatch(argv[argn], "-hamforce", 4) ) |
| 2029 |
forcemode = MODE_HAM; |
| 2030 |
else |
| 2031 |
if( pm_keymatch(argv[argn], "-nohamforce", 6) ) { |
| 2032 |
if( forcemode == MODE_HAM ) |
| 2033 |
forcemode = MODE_NONE; |
| 2034 |
} |
| 2035 |
else |
| 2036 |
if( pm_keymatch(argv[argn], "-24if", 4) ) { |
| 2037 |
ifmode = MODE_DEEP; |
| 2038 |
deepbits = 8; |
| 2039 |
} |
| 2040 |
else |
| 2041 |
if( pm_keymatch(argv[argn], "-no24if", 6) ) { |
| 2042 |
if( ifmode == MODE_DEEP ) |
| 2043 |
ifmode = MODE_NONE; |
| 2044 |
} |
| 2045 |
else |
| 2046 |
if( pm_keymatch(argv[argn], "-24force", 3) ) { |
| 2047 |
forcemode = MODE_DEEP; |
| 2048 |
deepbits = 8; |
| 2049 |
} |
| 2050 |
else |
| 2051 |
if( pm_keymatch(argv[argn], "-no24force", 5) ) { |
| 2052 |
if( forcemode == MODE_DEEP ) |
| 2053 |
forcemode = MODE_NONE; |
| 2054 |
} |
| 2055 |
else |
| 2056 |
if( pm_keymatch(argv[argn], "-deepplanes", 6) ) { |
| 2057 |
if( ++argn >= argc ) |
| 2058 |
pm_error("-deepplanes requires a value"); |
| 2059 |
deepbits = getIntVal(argv[argn], argv[argn-1], 3, 3*MAXPLANES); |
| 2060 |
if( deepbits % 3 != 0 ) |
| 2061 |
pm_error("option \"%s\" argument value must be divisible by 3", |
| 2062 |
argv[argn-1]); |
| 2063 |
deepbits /= 3; |
| 2064 |
} |
| 2065 |
else |
| 2066 |
if( pm_keymatch(argv[argn], "-deepbits", 6) ) { |
| 2067 |
if( ++argn >= argc ) |
| 2068 |
pm_error("-deepbits requires a value"); |
| 2069 |
deepbits = getIntVal(argv[argn], argv[argn-1], 1, MAXPLANES); |
| 2070 |
} |
| 2071 |
else |
| 2072 |
if( pm_keymatch(argv[argn], "-deepif", 6) ) |
| 2073 |
ifmode = MODE_DEEP; |
| 2074 |
else |
| 2075 |
if( pm_keymatch(argv[argn], "-nodeepif", 8) ) { |
| 2076 |
if( ifmode == MODE_DEEP ) |
| 2077 |
ifmode = MODE_NONE; |
| 2078 |
} |
| 2079 |
else |
| 2080 |
if( pm_keymatch(argv[argn], "-deepforce", 5) ) |
| 2081 |
forcemode = MODE_DEEP; |
| 2082 |
else |
| 2083 |
if( pm_keymatch(argv[argn], "-nodeepforce", 7) ) { |
| 2084 |
if( forcemode == MODE_DEEP ) |
| 2085 |
forcemode = MODE_NONE; |
| 2086 |
} |
| 2087 |
else |
| 2088 |
if( pm_keymatch(argv[argn], "-dcif", 4) ) |
| 2089 |
ifmode = MODE_DCOL; |
| 2090 |
else |
| 2091 |
if( pm_keymatch(argv[argn], "-nodcif", 6) ) { |
| 2092 |
if( ifmode == MODE_DCOL ) |
| 2093 |
ifmode = MODE_NONE; |
| 2094 |
} |
| 2095 |
else |
| 2096 |
if( pm_keymatch(argv[argn], "-dcforce", 4) ) |
| 2097 |
forcemode = MODE_DCOL; |
| 2098 |
else |
| 2099 |
if( pm_keymatch(argv[argn], "-nodcforce", 6) ) { |
| 2100 |
if( forcemode == MODE_DCOL ) |
| 2101 |
forcemode = MODE_NONE; |
| 2102 |
} |
| 2103 |
else |
| 2104 |
if( pm_keymatch(argv[argn], "-dcbits", 4) || |
| 2105 |
pm_keymatch(argv[argn], "-dcplanes", 4) ) { |
| 2106 |
if( argc - argn < 4 ) |
| 2107 |
pm_error("-dcbits requires 4 arguments"); |
| 2108 |
dcol.r = getIntVal(argv[argn+1], argv[argn], 1, MAXPLANES); |
| 2109 |
dcol.g = getIntVal(argv[argn+2], argv[argn], 1, MAXPLANES); |
| 2110 |
dcol.b = getIntVal(argv[argn+3], argv[argn], 1, MAXPLANES); |
| 2111 |
argn += 3; |
| 2112 |
} |
| 2113 |
else |
| 2114 |
if( pm_keymatch(argv[argn], "-normal", 4) ) { |
| 2115 |
ifmode = forcemode = MODE_NONE; |
| 2116 |
compmethod = DEF_COMPRESSION; |
| 2117 |
} |
| 2118 |
else |
| 2119 |
if( pm_keymatch(argv[argn], "-compress", 4) ) { |
| 2120 |
compr_force = 1; |
| 2121 |
if( compmethod == cmpNone ) |
| 2122 |
#if DEF_COMPRESSION == cmpNone |
| 2123 |
compmethod = cmpByteRun1; |
| 2124 |
#else |
| 2125 |
compmethod = DEF_COMPRESSION; |
| 2126 |
#endif |
| 2127 |
} |
| 2128 |
else |
| 2129 |
if( pm_keymatch(argv[argn], "-nocompress", 4) ) { |
| 2130 |
compr_force = 0; |
| 2131 |
compmethod = cmpNone; |
| 2132 |
} |
| 2133 |
else |
| 2134 |
if( pm_keymatch(argv[argn], "-cmethod", 4) ) { |
| 2135 |
if( ++argn >= argc ) |
| 2136 |
pm_error("-cmethod requires a value"); |
| 2137 |
compmethod = getComprMethod(argv[argn]); |
| 2138 |
} |
| 2139 |
else |
| 2140 |
if( pm_keymatch(argv[argn], "-nofloyd", 5) || |
| 2141 |
pm_keymatch(argv[argn], "-nofs", 5) ) |
| 2142 |
defunctArgs++; |
| 2143 |
else |
| 2144 |
if( pm_keymatch(argv[argn], "-annotation", 3) ) { |
| 2145 |
if( ++argn >= argc ) |
| 2146 |
pm_error("-annotation requires a value"); |
| 2147 |
anno_chunk = argv[argn]; |
| 2148 |
} |
| 2149 |
else |
| 2150 |
if( pm_keymatch(argv[argn], "-author", 3) ) { |
| 2151 |
if( ++argn >= argc ) |
| 2152 |
pm_error("-author requires a value"); |
| 2153 |
auth_chunk = argv[argn]; |
| 2154 |
} |
| 2155 |
else |
| 2156 |
if( pm_keymatch(argv[argn], "-copyright", 4) ) { |
| 2157 |
if( ++argn >= argc ) |
| 2158 |
pm_error("-copyright requires a value"); |
| 2159 |
copyr_chunk = argv[argn]; |
| 2160 |
} |
| 2161 |
else |
| 2162 |
if( pm_keymatch(argv[argn], "-name", 3) ) { |
| 2163 |
if( ++argn >= argc ) |
| 2164 |
pm_error("-name requires a value"); |
| 2165 |
name_chunk = argv[argn]; |
| 2166 |
} |
| 2167 |
else |
| 2168 |
if( pm_keymatch(argv[argn], "-text", 3) ) { |
| 2169 |
if( ++argn >= argc ) |
| 2170 |
pm_error("-text requires a value"); |
| 2171 |
text_chunk = argv[argn]; |
| 2172 |
} |
| 2173 |
else |
| 2174 |
pm_error("invalid option: %s", argv[argn]); |
| 2175 |
++argn; |
| 2176 |
} |
| 2177 |
|
| 2178 |
if( argn < argc ) { |
| 2179 |
ifP = pm_openr(argv[argn]); |
| 2180 |
++argn; |
| 2181 |
} |
| 2182 |
else |
| 2183 |
ifP = stdin; |
| 2184 |
|
| 2185 |
if( argn != argc ) |
| 2186 |
pm_error("Program takes no arguments."); |
| 2187 |
|
| 2188 |
mode = forcemode; |
| 2189 |
switch(forcemode) { |
| 2190 |
case MODE_HAM: |
| 2191 |
if (hammapmode == HAMMODE_RGB4 || hammapmode == HAMMODE_RGB5) |
| 2192 |
initRead(ifP, &cols, &rows, &maxval, &format, 1); |
| 2193 |
else |
| 2194 |
initRead(ifP, &cols, &rows, &maxval, &format, 0); |
| 2195 |
break; |
| 2196 |
case MODE_DCOL: |
| 2197 |
case MODE_DEEP: |
| 2198 |
mapfile = NULL; |
| 2199 |
initRead(ifP, &cols, &rows, &maxval, &format, 0); |
| 2200 |
break; |
| 2201 |
case MODE_RGB8: |
| 2202 |
mapfile = NULL; |
| 2203 |
initRead(ifP, &cols, &rows, &maxval, &format, 0); |
| 2204 |
break; |
| 2205 |
case MODE_RGBN: |
| 2206 |
mapfile = NULL; |
| 2207 |
initRead(ifP, &cols, &rows, &maxval, &format, 0); |
| 2208 |
break; |
| 2209 |
case MODE_CMAP: |
| 2210 |
|
| 2211 |
pm_message("computing colormap..."); |
| 2212 |
colormap = ppm_mapfiletocolorrow(ifP, MAXCOLORS, &colors, |
| 2213 |
&cmapmaxval); |
| 2214 |
if (colormap == NULL) |
| 2215 |
pm_error("too many colors - try doing a 'pnmquant %d'", |
| 2216 |
MAXCOLORS); |
| 2217 |
pm_message("%d colors found", colors); |
| 2218 |
break; |
| 2219 |
default: |
| 2220 |
if (mapfile) |
| 2221 |
initRead(ifP, &cols, &rows, &maxval, &format, 0); |
| 2222 |
else { |
| 2223 |
initRead(ifP, &cols, &rows, &maxval, &format, 1); |
| 2224 |
|
| 2225 |
pm_message("computing colormap..."); |
| 2226 |
colormap = |
| 2227 |
ppm_computecolorrow(pixels, cols, rows, MAXCOLORS, |
| 2228 |
&colors); |
| 2229 |
if (colormap) { |
| 2230 |
cmapmaxval = maxval; |
| 2231 |
pm_message("%d colors found", colors); |
| 2232 |
nPlanes = pm_maxvaltobits(colors-1); |
| 2233 |
if (fixplanes > nPlanes) |
| 2234 |
nPlanes = fixplanes; |
| 2235 |
} else { |
| 2236 |
mode = ifmode; |
| 2237 |
reportTooManyColors(ifmode, maxplanes, hamplanes, |
| 2238 |
dcol, deepbits ); |
| 2239 |
} |
| 2240 |
} |
| 2241 |
} |
| 2242 |
|
| 2243 |
if (mapfile) { |
| 2244 |
FILE * mapfp; |
| 2245 |
|
| 2246 |
pm_message("reading colormap file..."); |
| 2247 |
mapfp = pm_openr(mapfile); |
| 2248 |
colormap = ppm_mapfiletocolorrow(mapfp, MAXCOLORS, &colors, |
| 2249 |
&cmapmaxval); |
| 2250 |
pm_close(mapfp); |
| 2251 |
if (colormap == NULL) |
| 2252 |
pm_error("too many colors in mapfile for %d planes", maxplanes); |
| 2253 |
if (colors == 0) |
| 2254 |
pm_error("empty colormap??"); |
| 2255 |
pm_message("%d colors found in colormap", colors); |
| 2256 |
nPlanes = pm_maxvaltobits(colors-1); |
| 2257 |
} |
| 2258 |
|
| 2259 |
if (maskmethod != mskNone) { |
| 2260 |
if (transpname) { |
| 2261 |
MALLOCVAR_NOFAIL(transpColor); |
| 2262 |
*transpColor = ppm_parsecolor(transpname, maxval); |
| 2263 |
} |
| 2264 |
if (maskfile) { |
| 2265 |
int maskrows; |
| 2266 |
pbm_readpbminit(maskfile, &maskcols, &maskrows, &maskformat); |
| 2267 |
if (maskcols < cols || maskrows < rows) |
| 2268 |
pm_error("maskfile too small - try scaling it"); |
| 2269 |
if (maskcols > cols || maskrows > rows) |
| 2270 |
pm_message("warning - maskfile larger than image"); |
| 2271 |
} else |
| 2272 |
maskcols = rows; |
| 2273 |
maskrow = pbm_allocrow(maskcols); |
| 2274 |
} |
| 2275 |
|
| 2276 |
if (mode != MODE_CMAP) { |
| 2277 |
unsigned int i; |
| 2278 |
MALLOCARRAY_NOFAIL(coded_rowbuf, ilbm_rowByteCt(cols)); |
| 2279 |
for (i = 0; i < ilbm_rowByteCt(cols); ++i) |
| 2280 |
coded_rowbuf[i] = 0; |
| 2281 |
if (DO_COMPRESS) |
| 2282 |
pm_rlenc_allocoutbuf(&compr_rowbuf, |
| 2283 |
ilbm_rowByteCt(cols), |
| 2284 |
PM_RLE_PACKBITS); |
| 2285 |
} |
| 2286 |
|
| 2287 |
switch (mode) { |
| 2288 |
case MODE_HAM: |
| 2289 |
viewportmodes |= vmHAM; |
| 2290 |
ppmToHam(ifP, cols, rows, maxval, |
| 2291 |
colormap, colors, cmapmaxval, hamplanes); |
| 2292 |
break; |
| 2293 |
case MODE_DEEP: |
| 2294 |
ppmToDeep(ifP, cols, rows, maxval, deepbits); |
| 2295 |
break; |
| 2296 |
case MODE_DCOL: |
| 2297 |
ppmToDcol(ifP, cols, rows, maxval, &dcol); |
| 2298 |
break; |
| 2299 |
case MODE_RGB8: |
| 2300 |
ppmToRgb8(ifP, cols, rows, maxval); |
| 2301 |
break; |
| 2302 |
case MODE_RGBN: |
| 2303 |
ppmToRgbn(ifP, cols, rows, maxval); |
| 2304 |
break; |
| 2305 |
case MODE_CMAP: |
| 2306 |
ppmToCmap(colormap, colors, cmapmaxval); |
| 2307 |
break; |
| 2308 |
default: |
| 2309 |
ppmToStd(ifP, cols, rows, maxval, colormap, colors, |
| 2310 |
cmapmaxval, MAXCOLORS, nPlanes); |
| 2311 |
break; |
| 2312 |
} |
| 2313 |
pm_close(ifP); |
| 2314 |
return 0; |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
|