| 1 |
/* |
| 2 |
* Apply median cut on an image. |
| 3 |
* |
| 4 |
* tiffmedian [-c n] [-f] input output |
| 5 |
* -C n - set colortable size. Default is 256. |
| 6 |
* -f - use Floyd-Steinberg dithering. |
| 7 |
* -c lzw - compress output with LZW |
| 8 |
* -c none - use no compression on output |
| 9 |
* -c packbits - use packbits compression on output |
| 10 |
* -r n - create output with n rows/strip of data |
| 11 |
* (by default the compression scheme and rows/strip are taken |
| 12 |
* from the input file) |
| 13 |
* |
| 14 |
* Notes: |
| 15 |
* |
| 16 |
* [1] Floyd-Steinberg dither: |
| 17 |
* I should point out that the actual fractions we used were, assuming |
| 18 |
* you are at X, moving left to right: |
| 19 |
* |
| 20 |
* X 7/16 |
| 21 |
* 3/16 5/16 1/16 |
| 22 |
* |
| 23 |
* Note that the error goes to four neighbors, not three. I think this |
| 24 |
* will probably do better (at least for black and white) than the |
| 25 |
* 3/8-3/8-1/4 distribution, at the cost of greater processing. I have |
| 26 |
* seen the 3/8-3/8-1/4 distribution described as "our" algorithm before, |
| 27 |
* but I have no idea who the credit really belongs to. |
| 28 |
|
| 29 |
* Also, I should add that if you do zig-zag scanning (see my immediately |
| 30 |
* previous message), it is sufficient (but not quite as good) to send |
| 31 |
* half the error one pixel ahead (e.g. to the right on lines you scan |
| 32 |
* left to right), and half one pixel straight down. Again, this is for |
| 33 |
* black and white; I've not tried it with color. |
| 34 |
* -- |
| 35 |
* Lou Steinberg |
| 36 |
* |
| 37 |
* [2] Color Image Quantization for Frame Buffer Display, Paul Heckbert, |
| 38 |
* Siggraph '82 proceedings, pp. 297-307 |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "libport.h" |
| 42 |
#include "tif_config.h" |
| 43 |
|
| 44 |
#include <stdio.h> |
| 45 |
#include <stdlib.h> |
| 46 |
#include <string.h> |
| 47 |
|
| 48 |
#ifdef HAVE_UNISTD_H |
| 49 |
#include <unistd.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#include "tiffio.h" |
| 53 |
|
| 54 |
#ifndef EXIT_SUCCESS |
| 55 |
#define EXIT_SUCCESS 0 |
| 56 |
#endif |
| 57 |
#ifndef EXIT_FAILURE |
| 58 |
#define EXIT_FAILURE 1 |
| 59 |
#endif |
| 60 |
|
| 61 |
#define MAX_CMAP_SIZE 256 |
| 62 |
|
| 63 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 64 |
#define strneq(a, b, n) (strncmp(a, b, n) == 0) |
| 65 |
|
| 66 |
#define COLOR_DEPTH 8 |
| 67 |
#define MAX_COLOR 256 |
| 68 |
|
| 69 |
#define B_DEPTH 5 /* # bits/pixel to use */ |
| 70 |
#define B_LEN (1L << B_DEPTH) |
| 71 |
|
| 72 |
#define C_DEPTH 2 |
| 73 |
#define C_LEN (1L << C_DEPTH) /* # cells/color to use */ |
| 74 |
|
| 75 |
#define COLOR_SHIFT (COLOR_DEPTH - B_DEPTH) |
| 76 |
|
| 77 |
typedef struct colorbox |
| 78 |
{ |
| 79 |
struct colorbox *next, *prev; |
| 80 |
int rmin, rmax; |
| 81 |
int gmin, gmax; |
| 82 |
int bmin, bmax; |
| 83 |
uint32_t total; |
| 84 |
} Colorbox; |
| 85 |
|
| 86 |
typedef struct |
| 87 |
{ |
| 88 |
int num_ents; |
| 89 |
int entries[MAX_CMAP_SIZE][2]; |
| 90 |
} C_cell; |
| 91 |
|
| 92 |
static uint16_t rm[MAX_CMAP_SIZE], gm[MAX_CMAP_SIZE], bm[MAX_CMAP_SIZE]; |
| 93 |
static int num_colors; |
| 94 |
static uint32_t histogram[B_LEN][B_LEN][B_LEN]; |
| 95 |
static Colorbox *freeboxes; |
| 96 |
static Colorbox *usedboxes; |
| 97 |
static C_cell **ColorCells; |
| 98 |
static TIFF *in, *out; |
| 99 |
static uint32_t rowsperstrip = (uint32_t)-1; |
| 100 |
static uint16_t compression = (uint16_t)-1; |
| 101 |
static uint16_t bitspersample = 1; |
| 102 |
static uint16_t samplesperpixel; |
| 103 |
static uint32_t imagewidth; |
| 104 |
static uint32_t imagelength; |
| 105 |
static uint16_t predictor = 0; |
| 106 |
|
| 107 |
static void get_histogram(TIFF *, Colorbox *); |
| 108 |
static void splitbox(Colorbox *); |
| 109 |
static void shrinkbox(Colorbox *); |
| 110 |
static void map_colortable(void); |
| 111 |
static void quant(TIFF *, TIFF *); |
| 112 |
static void quant_fsdither(TIFF *, TIFF *); |
| 113 |
static Colorbox *largest_box(void); |
| 114 |
|
| 115 |
static void usage(int); |
| 116 |
static int processCompressOptions(char *); |
| 117 |
|
| 118 |
#define CopyField(tag, v) \ |
| 119 |
if (TIFFGetField(in, tag, &v)) \ |
| 120 |
TIFFSetField(out, tag, v) |
| 121 |
|
| 122 |
int main(int argc, char *argv[]) |
| 123 |
{ |
| 124 |
int i, dither = 0; |
| 125 |
uint16_t shortv, config, photometric; |
| 126 |
Colorbox *box_list, *ptr; |
| 127 |
float floatv; |
| 128 |
uint32_t longv; |
| 129 |
int c; |
| 130 |
#if !HAVE_DECL_OPTARG |
| 131 |
extern int optind; |
| 132 |
extern char *optarg; |
| 133 |
#endif |
| 134 |
|
| 135 |
num_colors = MAX_CMAP_SIZE; |
| 136 |
while ((c = getopt(argc, argv, "c:C:r:fh")) != -1) |
| 137 |
switch (c) |
| 138 |
{ |
| 139 |
case 'c': /* compression scheme */ |
| 140 |
if (!processCompressOptions(optarg)) |
| 141 |
usage(EXIT_FAILURE); |
| 142 |
break; |
| 143 |
case 'C': /* set colormap size */ |
| 144 |
num_colors = atoi(optarg); |
| 145 |
if (num_colors > MAX_CMAP_SIZE) |
| 146 |
{ |
| 147 |
fprintf(stderr, "-C: colormap too big, max %d\n", |
| 148 |
MAX_CMAP_SIZE); |
| 149 |
usage(EXIT_FAILURE); |
| 150 |
} |
| 151 |
if (num_colors < 2) |
| 152 |
{ |
| 153 |
fprintf(stderr, "-C: colormap too small, min %d\n", 2); |
| 154 |
usage(EXIT_FAILURE); |
| 155 |
} |
| 156 |
break; |
| 157 |
case 'f': /* dither */ |
| 158 |
dither = 1; |
| 159 |
break; |
| 160 |
case 'r': /* rows/strip */ |
| 161 |
rowsperstrip = atoi(optarg); |
| 162 |
break; |
| 163 |
case 'h': |
| 164 |
usage(EXIT_SUCCESS); |
| 165 |
/*NOTREACHED*/ |
| 166 |
break; |
| 167 |
case '?': |
| 168 |
usage(EXIT_FAILURE); |
| 169 |
/*NOTREACHED*/ |
| 170 |
break; |
| 171 |
} |
| 172 |
if (argc - optind != 2) |
| 173 |
usage(EXIT_FAILURE); |
| 174 |
in = TIFFOpen(argv[optind], "r"); |
| 175 |
if (in == NULL) |
| 176 |
return (EXIT_FAILURE); |
| 177 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imagewidth); |
| 178 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &imagelength); |
| 179 |
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bitspersample); |
| 180 |
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel); |
| 181 |
if (bitspersample != 8 && bitspersample != 16) |
| 182 |
{ |
| 183 |
fprintf(stderr, "%s: Image must have at least 8-bits/sample\n", |
| 184 |
argv[optind]); |
| 185 |
return (EXIT_FAILURE); |
| 186 |
} |
| 187 |
if (!TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric) || |
| 188 |
photometric != PHOTOMETRIC_RGB || samplesperpixel < 3) |
| 189 |
{ |
| 190 |
fprintf(stderr, "%s: Image must have RGB data\n", argv[optind]); |
| 191 |
return (EXIT_FAILURE); |
| 192 |
} |
| 193 |
TIFFGetField(in, TIFFTAG_PLANARCONFIG, &config); |
| 194 |
if (config != PLANARCONFIG_CONTIG) |
| 195 |
{ |
| 196 |
fprintf(stderr, "%s: Can only handle contiguous data packing\n", |
| 197 |
argv[optind]); |
| 198 |
return (EXIT_FAILURE); |
| 199 |
} |
| 200 |
|
| 201 |
/* |
| 202 |
* STEP 1: create empty boxes |
| 203 |
*/ |
| 204 |
usedboxes = NULL; |
| 205 |
box_list = freeboxes = |
| 206 |
(Colorbox *)_TIFFmalloc(num_colors * sizeof(Colorbox)); |
| 207 |
freeboxes[0].next = &freeboxes[1]; |
| 208 |
freeboxes[0].prev = NULL; |
| 209 |
for (i = 1; i < num_colors - 1; ++i) |
| 210 |
{ |
| 211 |
freeboxes[i].next = &freeboxes[i + 1]; |
| 212 |
freeboxes[i].prev = &freeboxes[i - 1]; |
| 213 |
} |
| 214 |
freeboxes[num_colors - 1].next = NULL; |
| 215 |
freeboxes[num_colors - 1].prev = &freeboxes[num_colors - 2]; |
| 216 |
|
| 217 |
/* |
| 218 |
* STEP 2: get histogram, initialize first box |
| 219 |
*/ |
| 220 |
ptr = freeboxes; |
| 221 |
freeboxes = ptr->next; |
| 222 |
if (freeboxes) |
| 223 |
freeboxes->prev = NULL; |
| 224 |
ptr->next = usedboxes; |
| 225 |
usedboxes = ptr; |
| 226 |
if (ptr->next) |
| 227 |
ptr->next->prev = ptr; |
| 228 |
get_histogram(in, ptr); |
| 229 |
|
| 230 |
/* |
| 231 |
* STEP 3: continually subdivide boxes until no more free |
| 232 |
* boxes remain or until all colors assigned. |
| 233 |
*/ |
| 234 |
while (freeboxes != NULL) |
| 235 |
{ |
| 236 |
ptr = largest_box(); |
| 237 |
if (ptr != NULL) |
| 238 |
splitbox(ptr); |
| 239 |
else |
| 240 |
freeboxes = NULL; |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* STEP 4: assign colors to all boxes |
| 245 |
*/ |
| 246 |
for (i = 0, ptr = usedboxes; ptr != NULL; ++i, ptr = ptr->next) |
| 247 |
{ |
| 248 |
rm[i] = ((ptr->rmin + ptr->rmax) << COLOR_SHIFT) / 2; |
| 249 |
gm[i] = ((ptr->gmin + ptr->gmax) << COLOR_SHIFT) / 2; |
| 250 |
bm[i] = ((ptr->bmin + ptr->bmax) << COLOR_SHIFT) / 2; |
| 251 |
} |
| 252 |
|
| 253 |
/* We're done with the boxes now */ |
| 254 |
_TIFFfree(box_list); |
| 255 |
freeboxes = usedboxes = NULL; |
| 256 |
|
| 257 |
/* |
| 258 |
* STEP 5: scan histogram and map all values to closest color |
| 259 |
*/ |
| 260 |
/* 5a: create cell list as described in Heckbert[2] */ |
| 261 |
ColorCells = |
| 262 |
(C_cell **)_TIFFmalloc(C_LEN * C_LEN * C_LEN * sizeof(C_cell *)); |
| 263 |
_TIFFmemset(ColorCells, 0, C_LEN * C_LEN * C_LEN * sizeof(C_cell *)); |
| 264 |
/* 5b: create mapping from truncated pixel space to color |
| 265 |
table entries */ |
| 266 |
map_colortable(); |
| 267 |
|
| 268 |
/* |
| 269 |
* STEP 6: scan image, match input values to table entries |
| 270 |
*/ |
| 271 |
out = TIFFOpen(argv[optind + 1], "w"); |
| 272 |
if (out == NULL) |
| 273 |
{ |
| 274 |
_TIFFfree(ColorCells); |
| 275 |
return (EXIT_FAILURE); |
| 276 |
} |
| 277 |
|
| 278 |
CopyField(TIFFTAG_SUBFILETYPE, longv); |
| 279 |
CopyField(TIFFTAG_IMAGEWIDTH, longv); |
| 280 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, (short)COLOR_DEPTH); |
| 281 |
if (compression != (uint16_t)-1) |
| 282 |
{ |
| 283 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 284 |
switch (compression) |
| 285 |
{ |
| 286 |
case COMPRESSION_LZW: |
| 287 |
case COMPRESSION_ADOBE_DEFLATE: |
| 288 |
case COMPRESSION_DEFLATE: |
| 289 |
if (predictor != 0) |
| 290 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 291 |
break; |
| 292 |
} |
| 293 |
} |
| 294 |
else |
| 295 |
CopyField(TIFFTAG_COMPRESSION, compression); |
| 296 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, (short)PHOTOMETRIC_PALETTE); |
| 297 |
CopyField(TIFFTAG_ORIENTATION, shortv); |
| 298 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, (short)1); |
| 299 |
CopyField(TIFFTAG_PLANARCONFIG, shortv); |
| 300 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, |
| 301 |
TIFFDefaultStripSize(out, rowsperstrip)); |
| 302 |
CopyField(TIFFTAG_MINSAMPLEVALUE, shortv); |
| 303 |
CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv); |
| 304 |
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv); |
| 305 |
CopyField(TIFFTAG_XRESOLUTION, floatv); |
| 306 |
CopyField(TIFFTAG_YRESOLUTION, floatv); |
| 307 |
CopyField(TIFFTAG_XPOSITION, floatv); |
| 308 |
CopyField(TIFFTAG_YPOSITION, floatv); |
| 309 |
|
| 310 |
if (dither) |
| 311 |
quant_fsdither(in, out); |
| 312 |
else |
| 313 |
quant(in, out); |
| 314 |
/* |
| 315 |
* Scale colormap to TIFF-required 16-bit values. |
| 316 |
*/ |
| 317 |
#define SCALE(x) (((x) * ((1L << 16) - 1)) / 255) |
| 318 |
for (i = 0; i < MAX_CMAP_SIZE; ++i) |
| 319 |
{ |
| 320 |
rm[i] = SCALE(rm[i]); |
| 321 |
gm[i] = SCALE(gm[i]); |
| 322 |
bm[i] = SCALE(bm[i]); |
| 323 |
} |
| 324 |
TIFFSetField(out, TIFFTAG_COLORMAP, rm, gm, bm); |
| 325 |
(void)TIFFClose(out); |
| 326 |
_TIFFfree(ColorCells); |
| 327 |
return (EXIT_SUCCESS); |
| 328 |
} |
| 329 |
|
| 330 |
static int processCompressOptions(char *opt) |
| 331 |
{ |
| 332 |
if (streq(opt, "none")) |
| 333 |
compression = COMPRESSION_NONE; |
| 334 |
else if (streq(opt, "packbits")) |
| 335 |
compression = COMPRESSION_PACKBITS; |
| 336 |
else if (strneq(opt, "lzw", 3)) |
| 337 |
{ |
| 338 |
char *cp = strchr(opt, ':'); |
| 339 |
if (cp) |
| 340 |
predictor = atoi(cp + 1); |
| 341 |
compression = COMPRESSION_LZW; |
| 342 |
} |
| 343 |
else if (strneq(opt, "zip", 3)) |
| 344 |
{ |
| 345 |
char *cp = strchr(opt, ':'); |
| 346 |
if (cp) |
| 347 |
predictor = atoi(cp + 1); |
| 348 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 349 |
} |
| 350 |
else |
| 351 |
return (0); |
| 352 |
return (1); |
| 353 |
} |
| 354 |
|
| 355 |
static const char usage_info[] = |
| 356 |
"Apply the median cut algorithm to an RGB TIFF file\n\n" |
| 357 |
"usage: tiffmedian [options] input.tif output.tif\n" |
| 358 |
"where options are:\n" |
| 359 |
" -r # make each strip have no more than # rows\n" |
| 360 |
" -C # create a colormap with # entries\n" |
| 361 |
" -f use Floyd-Steinberg dithering\n" |
| 362 |
"\n" |
| 363 |
#ifdef LZW_SUPPORT |
| 364 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n" |
| 365 |
/* " LZW options:" */ |
| 366 |
" # set predictor value\n" |
| 367 |
" For example, -c lzw:2 to get LZW-encoded data with horizontal " |
| 368 |
"differencing\n" |
| 369 |
#endif |
| 370 |
#ifdef ZIP_SUPPORT |
| 371 |
" -c zip[:opts] compress output with deflate encoding\n" |
| 372 |
/* " Deflate (ZIP) options:" */ |
| 373 |
" # set predictor value\n" |
| 374 |
#endif |
| 375 |
#ifdef PACKBITS_SUPPORT |
| 376 |
" -c packbits compress output with packbits encoding\n" |
| 377 |
#endif |
| 378 |
#if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || defined(PACKBITS_SUPPORT) |
| 379 |
" -c none use no compression algorithm on output\n" |
| 380 |
#endif |
| 381 |
"\n"; |
| 382 |
|
| 383 |
static void usage(int code) |
| 384 |
{ |
| 385 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 386 |
|
| 387 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 388 |
fprintf(out, "%s", usage_info); |
| 389 |
exit(code); |
| 390 |
} |
| 391 |
|
| 392 |
static void get_histogram(TIFF *in, Colorbox *box) |
| 393 |
{ |
| 394 |
register unsigned char *inptr; |
| 395 |
register int red, green, blue; |
| 396 |
register uint32_t j, i; |
| 397 |
unsigned char *inputline; |
| 398 |
|
| 399 |
inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); |
| 400 |
if (inputline == NULL) |
| 401 |
{ |
| 402 |
fprintf(stderr, "No space for scanline buffer\n"); |
| 403 |
exit(EXIT_FAILURE); |
| 404 |
} |
| 405 |
box->rmin = box->gmin = box->bmin = 999; |
| 406 |
box->rmax = box->gmax = box->bmax = -1; |
| 407 |
box->total = imagewidth * imagelength; |
| 408 |
|
| 409 |
{ |
| 410 |
register uint32_t *ptr = &histogram[0][0][0]; |
| 411 |
for (i = B_LEN * B_LEN * B_LEN; i-- > 0;) |
| 412 |
*ptr++ = 0; |
| 413 |
} |
| 414 |
for (i = 0; i < imagelength; i++) |
| 415 |
{ |
| 416 |
if (TIFFReadScanline(in, inputline, i, 0) <= 0) |
| 417 |
break; |
| 418 |
inptr = inputline; |
| 419 |
for (j = imagewidth; j-- > 0;) |
| 420 |
{ |
| 421 |
red = (*inptr++) & 0xff >> COLOR_SHIFT; |
| 422 |
green = (*inptr++) & 0xff >> COLOR_SHIFT; |
| 423 |
blue = (*inptr++) & 0xff >> COLOR_SHIFT; |
| 424 |
if ((red | green | blue) >= B_LEN) |
| 425 |
{ |
| 426 |
fprintf(stderr, "Logic error. " |
| 427 |
"Histogram array overflow!\n"); |
| 428 |
exit(EXIT_FAILURE); |
| 429 |
} |
| 430 |
if (red < box->rmin) |
| 431 |
box->rmin = red; |
| 432 |
if (red > box->rmax) |
| 433 |
box->rmax = red; |
| 434 |
if (green < box->gmin) |
| 435 |
box->gmin = green; |
| 436 |
if (green > box->gmax) |
| 437 |
box->gmax = green; |
| 438 |
if (blue < box->bmin) |
| 439 |
box->bmin = blue; |
| 440 |
if (blue > box->bmax) |
| 441 |
box->bmax = blue; |
| 442 |
histogram[red][green][blue]++; |
| 443 |
} |
| 444 |
} |
| 445 |
_TIFFfree(inputline); |
| 446 |
} |
| 447 |
|
| 448 |
static Colorbox *largest_box(void) |
| 449 |
{ |
| 450 |
register Colorbox *p, *b; |
| 451 |
register uint32_t size; |
| 452 |
|
| 453 |
b = NULL; |
| 454 |
size = 0; |
| 455 |
for (p = usedboxes; p != NULL; p = p->next) |
| 456 |
if ((p->rmax > p->rmin || p->gmax > p->gmin || p->bmax > p->bmin) && |
| 457 |
p->total > size) |
| 458 |
size = (b = p)->total; |
| 459 |
return (b); |
| 460 |
} |
| 461 |
|
| 462 |
static void splitbox(Colorbox *ptr) |
| 463 |
{ |
| 464 |
uint32_t hist2[B_LEN]; |
| 465 |
int first = 0, last = 0; |
| 466 |
register Colorbox *new; |
| 467 |
register uint32_t *iptr, *histp; |
| 468 |
register int i, j; |
| 469 |
register int ir, ig, ib; |
| 470 |
register uint32_t sum, sum1, sum2; |
| 471 |
enum |
| 472 |
{ |
| 473 |
RED, |
| 474 |
GREEN, |
| 475 |
BLUE |
| 476 |
} axis; |
| 477 |
|
| 478 |
/* |
| 479 |
* See which axis is the largest, do a histogram along that |
| 480 |
* axis. Split at median point. Contract both new boxes to |
| 481 |
* fit points and return |
| 482 |
*/ |
| 483 |
i = ptr->rmax - ptr->rmin; |
| 484 |
if (i >= ptr->gmax - ptr->gmin && i >= ptr->bmax - ptr->bmin) |
| 485 |
axis = RED; |
| 486 |
else if (ptr->gmax - ptr->gmin >= ptr->bmax - ptr->bmin) |
| 487 |
axis = GREEN; |
| 488 |
else |
| 489 |
axis = BLUE; |
| 490 |
/* get histogram along longest axis */ |
| 491 |
switch (axis) |
| 492 |
{ |
| 493 |
case RED: |
| 494 |
histp = &hist2[ptr->rmin]; |
| 495 |
for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
| 496 |
{ |
| 497 |
*histp = 0; |
| 498 |
for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
| 499 |
{ |
| 500 |
iptr = &histogram[ir][ig][ptr->bmin]; |
| 501 |
for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
| 502 |
*histp += *iptr++; |
| 503 |
} |
| 504 |
histp++; |
| 505 |
} |
| 506 |
first = ptr->rmin; |
| 507 |
last = ptr->rmax; |
| 508 |
break; |
| 509 |
case GREEN: |
| 510 |
histp = &hist2[ptr->gmin]; |
| 511 |
for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
| 512 |
{ |
| 513 |
*histp = 0; |
| 514 |
for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
| 515 |
{ |
| 516 |
iptr = &histogram[ir][ig][ptr->bmin]; |
| 517 |
for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
| 518 |
*histp += *iptr++; |
| 519 |
} |
| 520 |
histp++; |
| 521 |
} |
| 522 |
first = ptr->gmin; |
| 523 |
last = ptr->gmax; |
| 524 |
break; |
| 525 |
case BLUE: |
| 526 |
histp = &hist2[ptr->bmin]; |
| 527 |
for (ib = ptr->bmin; ib <= ptr->bmax; ++ib) |
| 528 |
{ |
| 529 |
*histp = 0; |
| 530 |
for (ir = ptr->rmin; ir <= ptr->rmax; ++ir) |
| 531 |
{ |
| 532 |
iptr = &histogram[ir][ptr->gmin][ib]; |
| 533 |
for (ig = ptr->gmin; ig <= ptr->gmax; ++ig) |
| 534 |
{ |
| 535 |
*histp += *iptr; |
| 536 |
iptr += B_LEN; |
| 537 |
} |
| 538 |
} |
| 539 |
histp++; |
| 540 |
} |
| 541 |
first = ptr->bmin; |
| 542 |
last = ptr->bmax; |
| 543 |
break; |
| 544 |
} |
| 545 |
/* find median point */ |
| 546 |
sum2 = ptr->total / 2; |
| 547 |
histp = &hist2[first]; |
| 548 |
sum = 0; |
| 549 |
for (i = first; i <= last && (sum += *histp++) < sum2; ++i) |
| 550 |
; |
| 551 |
if (i == first) |
| 552 |
i++; |
| 553 |
|
| 554 |
/* Create new box, re-allocate points */ |
| 555 |
new = freeboxes; |
| 556 |
freeboxes = new->next; |
| 557 |
if (freeboxes) |
| 558 |
freeboxes->prev = NULL; |
| 559 |
if (usedboxes) |
| 560 |
usedboxes->prev = new; |
| 561 |
new->next = usedboxes; |
| 562 |
usedboxes = new; |
| 563 |
|
| 564 |
histp = &hist2[first]; |
| 565 |
for (sum1 = 0, j = first; j < i; j++) |
| 566 |
sum1 += *histp++; |
| 567 |
for (sum2 = 0, j = i; j <= last; j++) |
| 568 |
sum2 += *histp++; |
| 569 |
new->total = sum1; |
| 570 |
ptr->total = sum2; |
| 571 |
|
| 572 |
new->rmin = ptr->rmin; |
| 573 |
new->rmax = ptr->rmax; |
| 574 |
new->gmin = ptr->gmin; |
| 575 |
new->gmax = ptr->gmax; |
| 576 |
new->bmin = ptr->bmin; |
| 577 |
new->bmax = ptr->bmax; |
| 578 |
switch (axis) |
| 579 |
{ |
| 580 |
case RED: |
| 581 |
new->rmax = i - 1; |
| 582 |
ptr->rmin = i; |
| 583 |
break; |
| 584 |
case GREEN: |
| 585 |
new->gmax = i - 1; |
| 586 |
ptr->gmin = i; |
| 587 |
break; |
| 588 |
case BLUE: |
| 589 |
new->bmax = i - 1; |
| 590 |
ptr->bmin = i; |
| 591 |
break; |
| 592 |
} |
| 593 |
shrinkbox(new); |
| 594 |
shrinkbox(ptr); |
| 595 |
} |
| 596 |
|
| 597 |
static void shrinkbox(Colorbox *box) |
| 598 |
{ |
| 599 |
register uint32_t *histp; |
| 600 |
register int ir, ig, ib; |
| 601 |
|
| 602 |
if (box->rmax > box->rmin) |
| 603 |
{ |
| 604 |
for (ir = box->rmin; ir <= box->rmax; ++ir) |
| 605 |
for (ig = box->gmin; ig <= box->gmax; ++ig) |
| 606 |
{ |
| 607 |
histp = &histogram[ir][ig][box->bmin]; |
| 608 |
for (ib = box->bmin; ib <= box->bmax; ++ib) |
| 609 |
if (*histp++ != 0) |
| 610 |
{ |
| 611 |
box->rmin = ir; |
| 612 |
goto have_rmin; |
| 613 |
} |
| 614 |
} |
| 615 |
have_rmin: |
| 616 |
if (box->rmax > box->rmin) |
| 617 |
for (ir = box->rmax; ir >= box->rmin; --ir) |
| 618 |
for (ig = box->gmin; ig <= box->gmax; ++ig) |
| 619 |
{ |
| 620 |
histp = &histogram[ir][ig][box->bmin]; |
| 621 |
ib = box->bmin; |
| 622 |
for (; ib <= box->bmax; ++ib) |
| 623 |
if (*histp++ != 0) |
| 624 |
{ |
| 625 |
box->rmax = ir; |
| 626 |
goto have_rmax; |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
have_rmax: |
| 631 |
if (box->gmax > box->gmin) |
| 632 |
{ |
| 633 |
for (ig = box->gmin; ig <= box->gmax; ++ig) |
| 634 |
for (ir = box->rmin; ir <= box->rmax; ++ir) |
| 635 |
{ |
| 636 |
histp = &histogram[ir][ig][box->bmin]; |
| 637 |
for (ib = box->bmin; ib <= box->bmax; ++ib) |
| 638 |
if (*histp++ != 0) |
| 639 |
{ |
| 640 |
box->gmin = ig; |
| 641 |
goto have_gmin; |
| 642 |
} |
| 643 |
} |
| 644 |
have_gmin: |
| 645 |
if (box->gmax > box->gmin) |
| 646 |
for (ig = box->gmax; ig >= box->gmin; --ig) |
| 647 |
for (ir = box->rmin; ir <= box->rmax; ++ir) |
| 648 |
{ |
| 649 |
histp = &histogram[ir][ig][box->bmin]; |
| 650 |
ib = box->bmin; |
| 651 |
for (; ib <= box->bmax; ++ib) |
| 652 |
if (*histp++ != 0) |
| 653 |
{ |
| 654 |
box->gmax = ig; |
| 655 |
goto have_gmax; |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
have_gmax: |
| 660 |
if (box->bmax > box->bmin) |
| 661 |
{ |
| 662 |
for (ib = box->bmin; ib <= box->bmax; ++ib) |
| 663 |
for (ir = box->rmin; ir <= box->rmax; ++ir) |
| 664 |
{ |
| 665 |
histp = &histogram[ir][box->gmin][ib]; |
| 666 |
for (ig = box->gmin; ig <= box->gmax; ++ig) |
| 667 |
{ |
| 668 |
if (*histp != 0) |
| 669 |
{ |
| 670 |
box->bmin = ib; |
| 671 |
goto have_bmin; |
| 672 |
} |
| 673 |
histp += B_LEN; |
| 674 |
} |
| 675 |
} |
| 676 |
have_bmin: |
| 677 |
if (box->bmax > box->bmin) |
| 678 |
for (ib = box->bmax; ib >= box->bmin; --ib) |
| 679 |
for (ir = box->rmin; ir <= box->rmax; ++ir) |
| 680 |
{ |
| 681 |
histp = &histogram[ir][box->gmin][ib]; |
| 682 |
ig = box->gmin; |
| 683 |
for (; ig <= box->gmax; ++ig) |
| 684 |
{ |
| 685 |
if (*histp != 0) |
| 686 |
{ |
| 687 |
box->bmax = ib; |
| 688 |
goto have_bmax; |
| 689 |
} |
| 690 |
histp += B_LEN; |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
have_bmax:; |
| 695 |
} |
| 696 |
|
| 697 |
static C_cell *create_colorcell(int red, int green, int blue) |
| 698 |
{ |
| 699 |
register int ir, ig, ib, i; |
| 700 |
register C_cell *ptr; |
| 701 |
int mindist, next_n; |
| 702 |
register int tmp, dist, n; |
| 703 |
|
| 704 |
ir = red >> (COLOR_DEPTH - C_DEPTH); |
| 705 |
ig = green >> (COLOR_DEPTH - C_DEPTH); |
| 706 |
ib = blue >> (COLOR_DEPTH - C_DEPTH); |
| 707 |
ptr = (C_cell *)_TIFFmalloc(sizeof(C_cell)); |
| 708 |
*(ColorCells + ir * C_LEN * C_LEN + ig * C_LEN + ib) = ptr; |
| 709 |
ptr->num_ents = 0; |
| 710 |
|
| 711 |
/* |
| 712 |
* Step 1: find all colors inside this cell, while we're at |
| 713 |
* it, find distance of centermost point to furthest corner |
| 714 |
*/ |
| 715 |
mindist = 99999999; |
| 716 |
for (i = 0; i < num_colors; ++i) |
| 717 |
{ |
| 718 |
if (rm[i] >> (COLOR_DEPTH - C_DEPTH) != ir || |
| 719 |
gm[i] >> (COLOR_DEPTH - C_DEPTH) != ig || |
| 720 |
bm[i] >> (COLOR_DEPTH - C_DEPTH) != ib) |
| 721 |
continue; |
| 722 |
ptr->entries[ptr->num_ents][0] = i; |
| 723 |
ptr->entries[ptr->num_ents][1] = 0; |
| 724 |
++ptr->num_ents; |
| 725 |
tmp = rm[i] - red; |
| 726 |
if (tmp < (MAX_COLOR / C_LEN / 2)) |
| 727 |
tmp = MAX_COLOR / C_LEN - 1 - tmp; |
| 728 |
dist = tmp * tmp; |
| 729 |
tmp = gm[i] - green; |
| 730 |
if (tmp < (MAX_COLOR / C_LEN / 2)) |
| 731 |
tmp = MAX_COLOR / C_LEN - 1 - tmp; |
| 732 |
dist += tmp * tmp; |
| 733 |
tmp = bm[i] - blue; |
| 734 |
if (tmp < (MAX_COLOR / C_LEN / 2)) |
| 735 |
tmp = MAX_COLOR / C_LEN - 1 - tmp; |
| 736 |
dist += tmp * tmp; |
| 737 |
if (dist < mindist) |
| 738 |
mindist = dist; |
| 739 |
} |
| 740 |
|
| 741 |
/* |
| 742 |
* Step 3: find all points within that distance to cell. |
| 743 |
*/ |
| 744 |
for (i = 0; i < num_colors; ++i) |
| 745 |
{ |
| 746 |
if (rm[i] >> (COLOR_DEPTH - C_DEPTH) == ir && |
| 747 |
gm[i] >> (COLOR_DEPTH - C_DEPTH) == ig && |
| 748 |
bm[i] >> (COLOR_DEPTH - C_DEPTH) == ib) |
| 749 |
continue; |
| 750 |
dist = 0; |
| 751 |
if ((tmp = red - rm[i]) > 0 || |
| 752 |
(tmp = rm[i] - (red + MAX_COLOR / C_LEN - 1)) > 0) |
| 753 |
dist += tmp * tmp; |
| 754 |
if ((tmp = green - gm[i]) > 0 || |
| 755 |
(tmp = gm[i] - (green + MAX_COLOR / C_LEN - 1)) > 0) |
| 756 |
dist += tmp * tmp; |
| 757 |
if ((tmp = blue - bm[i]) > 0 || |
| 758 |
(tmp = bm[i] - (blue + MAX_COLOR / C_LEN - 1)) > 0) |
| 759 |
dist += tmp * tmp; |
| 760 |
if (dist < mindist) |
| 761 |
{ |
| 762 |
ptr->entries[ptr->num_ents][0] = i; |
| 763 |
ptr->entries[ptr->num_ents][1] = dist; |
| 764 |
++ptr->num_ents; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
/* |
| 769 |
* Sort color cells by distance, use cheap exchange sort |
| 770 |
*/ |
| 771 |
for (n = ptr->num_ents - 1; n > 0; n = next_n) |
| 772 |
{ |
| 773 |
next_n = 0; |
| 774 |
for (i = 0; i < n; ++i) |
| 775 |
if (ptr->entries[i][1] > ptr->entries[i + 1][1]) |
| 776 |
{ |
| 777 |
tmp = ptr->entries[i][0]; |
| 778 |
ptr->entries[i][0] = ptr->entries[i + 1][0]; |
| 779 |
ptr->entries[i + 1][0] = tmp; |
| 780 |
tmp = ptr->entries[i][1]; |
| 781 |
ptr->entries[i][1] = ptr->entries[i + 1][1]; |
| 782 |
ptr->entries[i + 1][1] = tmp; |
| 783 |
next_n = i; |
| 784 |
} |
| 785 |
} |
| 786 |
return (ptr); |
| 787 |
} |
| 788 |
|
| 789 |
static void map_colortable(void) |
| 790 |
{ |
| 791 |
register uint32_t *histp = &histogram[0][0][0]; |
| 792 |
register C_cell *cell = NULL; |
| 793 |
register int j, tmp, d2, dist; |
| 794 |
int ir, ig, ib, i; |
| 795 |
|
| 796 |
for (ir = 0; ir < B_LEN; ++ir) |
| 797 |
for (ig = 0; ig < B_LEN; ++ig) |
| 798 |
for (ib = 0; ib < B_LEN; ++ib, histp++) |
| 799 |
{ |
| 800 |
if (*histp == 0) |
| 801 |
{ |
| 802 |
*histp = -1; |
| 803 |
continue; |
| 804 |
} |
| 805 |
cell = *(ColorCells + |
| 806 |
(((ir >> (B_DEPTH - C_DEPTH)) << C_DEPTH * 2) + |
| 807 |
((ig >> (B_DEPTH - C_DEPTH)) << C_DEPTH) + |
| 808 |
(ib >> (B_DEPTH - C_DEPTH)))); |
| 809 |
if (cell == NULL) |
| 810 |
cell = |
| 811 |
create_colorcell(ir << COLOR_SHIFT, ig << COLOR_SHIFT, |
| 812 |
ib << COLOR_SHIFT); |
| 813 |
dist = 9999999; |
| 814 |
for (i = 0; i < cell->num_ents && dist > cell->entries[i][1]; |
| 815 |
++i) |
| 816 |
{ |
| 817 |
j = cell->entries[i][0]; |
| 818 |
d2 = rm[j] - (ir << COLOR_SHIFT); |
| 819 |
d2 *= d2; |
| 820 |
tmp = gm[j] - (ig << COLOR_SHIFT); |
| 821 |
d2 += tmp * tmp; |
| 822 |
tmp = bm[j] - (ib << COLOR_SHIFT); |
| 823 |
d2 += tmp * tmp; |
| 824 |
if (d2 < dist) |
| 825 |
{ |
| 826 |
dist = d2; |
| 827 |
*histp = j; |
| 828 |
} |
| 829 |
} |
| 830 |
} |
| 831 |
_TIFFfree(cell); |
| 832 |
} |
| 833 |
|
| 834 |
/* |
| 835 |
* straight quantization. Each pixel is mapped to the colors |
| 836 |
* closest to it. Color values are rounded to the nearest color |
| 837 |
* table entry. |
| 838 |
*/ |
| 839 |
static void quant(TIFF *in, TIFF *out) |
| 840 |
{ |
| 841 |
unsigned char *outline, *inputline; |
| 842 |
register unsigned char *outptr, *inptr; |
| 843 |
register uint32_t i, j; |
| 844 |
register int red, green, blue; |
| 845 |
|
| 846 |
inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); |
| 847 |
outline = (unsigned char *)_TIFFmalloc(imagewidth); |
| 848 |
for (i = 0; i < imagelength; i++) |
| 849 |
{ |
| 850 |
if (TIFFReadScanline(in, inputline, i, 0) <= 0) |
| 851 |
break; |
| 852 |
inptr = inputline; |
| 853 |
outptr = outline; |
| 854 |
for (j = 0; j < imagewidth; j++) |
| 855 |
{ |
| 856 |
red = *inptr++ >> COLOR_SHIFT; |
| 857 |
green = *inptr++ >> COLOR_SHIFT; |
| 858 |
blue = *inptr++ >> COLOR_SHIFT; |
| 859 |
*outptr++ = (unsigned char)histogram[red][green][blue]; |
| 860 |
} |
| 861 |
if (TIFFWriteScanline(out, outline, i, 0) < 0) |
| 862 |
break; |
| 863 |
} |
| 864 |
_TIFFfree(inputline); |
| 865 |
_TIFFfree(outline); |
| 866 |
} |
| 867 |
|
| 868 |
#define SWAP(type, a, b) \ |
| 869 |
{ \ |
| 870 |
type p; \ |
| 871 |
p = a; \ |
| 872 |
a = b; \ |
| 873 |
b = p; \ |
| 874 |
} |
| 875 |
|
| 876 |
#define GetInputLine(tif, row, bad) \ |
| 877 |
do \ |
| 878 |
{ \ |
| 879 |
if (TIFFReadScanline(tif, inputline, row, 0) <= 0) \ |
| 880 |
bad; \ |
| 881 |
inptr = inputline; \ |
| 882 |
nextptr = nextline; \ |
| 883 |
for (j = 0; j < imagewidth; ++j) \ |
| 884 |
{ \ |
| 885 |
*nextptr++ = *inptr++; \ |
| 886 |
*nextptr++ = *inptr++; \ |
| 887 |
*nextptr++ = *inptr++; \ |
| 888 |
} \ |
| 889 |
} while (0); |
| 890 |
#define GetComponent(raw, cshift, c) \ |
| 891 |
do \ |
| 892 |
{ \ |
| 893 |
cshift = raw; \ |
| 894 |
if (cshift < 0) \ |
| 895 |
cshift = 0; \ |
| 896 |
else if (cshift >= MAX_COLOR) \ |
| 897 |
cshift = MAX_COLOR - 1; \ |
| 898 |
c = cshift; \ |
| 899 |
cshift >>= COLOR_SHIFT; \ |
| 900 |
} while (0); |
| 901 |
|
| 902 |
static void quant_fsdither(TIFF *in, TIFF *out) |
| 903 |
{ |
| 904 |
unsigned char *outline, *inputline, *inptr; |
| 905 |
short *thisline, *nextline; |
| 906 |
register unsigned char *outptr; |
| 907 |
register short *thisptr, *nextptr; |
| 908 |
register uint32_t i, j; |
| 909 |
uint32_t imax, jmax; |
| 910 |
int lastline, lastpixel; |
| 911 |
|
| 912 |
imax = imagelength - 1; |
| 913 |
jmax = imagewidth - 1; |
| 914 |
inputline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(in)); |
| 915 |
thisline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof(short)); |
| 916 |
nextline = (short *)_TIFFmalloc(imagewidth * 3 * sizeof(short)); |
| 917 |
outline = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out)); |
| 918 |
|
| 919 |
GetInputLine(in, 0, goto bad); /* get first line */ |
| 920 |
for (i = 1; i <= imagelength; ++i) |
| 921 |
{ |
| 922 |
SWAP(short *, thisline, nextline); |
| 923 |
lastline = (i >= imax); |
| 924 |
if (i <= imax) |
| 925 |
GetInputLine(in, i, break); |
| 926 |
thisptr = thisline; |
| 927 |
nextptr = nextline; |
| 928 |
outptr = outline; |
| 929 |
for (j = 0; j < imagewidth; ++j) |
| 930 |
{ |
| 931 |
int red, green, blue; |
| 932 |
register int oval, r2, g2, b2; |
| 933 |
|
| 934 |
lastpixel = (j == jmax); |
| 935 |
GetComponent(*thisptr++, r2, red); |
| 936 |
GetComponent(*thisptr++, g2, green); |
| 937 |
GetComponent(*thisptr++, b2, blue); |
| 938 |
oval = histogram[r2][g2][b2]; |
| 939 |
if (oval == -1) |
| 940 |
{ |
| 941 |
int ci; |
| 942 |
register int cj, tmp, d2, dist; |
| 943 |
register C_cell *cell; |
| 944 |
|
| 945 |
cell = *(ColorCells + |
| 946 |
(((r2 >> (B_DEPTH - C_DEPTH)) << C_DEPTH * 2) + |
| 947 |
((g2 >> (B_DEPTH - C_DEPTH)) << C_DEPTH) + |
| 948 |
(b2 >> (B_DEPTH - C_DEPTH)))); |
| 949 |
if (cell == NULL) |
| 950 |
cell = create_colorcell(red, green, blue); |
| 951 |
dist = 9999999; |
| 952 |
for (ci = 0; ci < cell->num_ents && dist > cell->entries[ci][1]; |
| 953 |
++ci) |
| 954 |
{ |
| 955 |
cj = cell->entries[ci][0]; |
| 956 |
d2 = (rm[cj] >> COLOR_SHIFT) - r2; |
| 957 |
d2 *= d2; |
| 958 |
tmp = (gm[cj] >> COLOR_SHIFT) - g2; |
| 959 |
d2 += tmp * tmp; |
| 960 |
tmp = (bm[cj] >> COLOR_SHIFT) - b2; |
| 961 |
d2 += tmp * tmp; |
| 962 |
if (d2 < dist) |
| 963 |
{ |
| 964 |
dist = d2; |
| 965 |
oval = cj; |
| 966 |
} |
| 967 |
} |
| 968 |
histogram[r2][g2][b2] = oval; |
| 969 |
} |
| 970 |
*outptr++ = oval; |
| 971 |
red -= rm[oval]; |
| 972 |
green -= gm[oval]; |
| 973 |
blue -= bm[oval]; |
| 974 |
if (!lastpixel) |
| 975 |
{ |
| 976 |
thisptr[0] += blue * 7 / 16; |
| 977 |
thisptr[1] += green * 7 / 16; |
| 978 |
thisptr[2] += red * 7 / 16; |
| 979 |
} |
| 980 |
if (!lastline) |
| 981 |
{ |
| 982 |
if (j != 0) |
| 983 |
{ |
| 984 |
nextptr[-3] += blue * 3 / 16; |
| 985 |
nextptr[-2] += green * 3 / 16; |
| 986 |
nextptr[-1] += red * 3 / 16; |
| 987 |
} |
| 988 |
nextptr[0] += blue * 5 / 16; |
| 989 |
nextptr[1] += green * 5 / 16; |
| 990 |
nextptr[2] += red * 5 / 16; |
| 991 |
if (!lastpixel) |
| 992 |
{ |
| 993 |
nextptr[3] += blue / 16; |
| 994 |
nextptr[4] += green / 16; |
| 995 |
nextptr[5] += red / 16; |
| 996 |
} |
| 997 |
nextptr += 3; |
| 998 |
} |
| 999 |
} |
| 1000 |
if (TIFFWriteScanline(out, outline, i - 1, 0) < 0) |
| 1001 |
break; |
| 1002 |
} |
| 1003 |
bad: |
| 1004 |
_TIFFfree(inputline); |
| 1005 |
_TIFFfree(thisline); |
| 1006 |
_TIFFfree(nextline); |
| 1007 |
_TIFFfree(outline); |
| 1008 |
} |