| 1 |
/* |
| 2 |
* Copyright (c) 1994-1997 Sam Leffler |
| 3 |
* Copyright (c) 1994-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "libport.h" |
| 26 |
#include "tif_config.h" |
| 27 |
|
| 28 |
#include <math.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#ifdef HAVE_UNISTD_H |
| 34 |
#include <unistd.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "tiffio.h" |
| 38 |
|
| 39 |
#ifndef EXIT_SUCCESS |
| 40 |
#define EXIT_SUCCESS 0 |
| 41 |
#endif |
| 42 |
#ifndef EXIT_FAILURE |
| 43 |
#define EXIT_FAILURE 1 |
| 44 |
#endif |
| 45 |
|
| 46 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 47 |
|
| 48 |
#ifndef TIFFhowmany8 |
| 49 |
#define TIFFhowmany8(x) \ |
| 50 |
(((x)&0x07) ? ((uint32_t)(x) >> 3) + 1 : (uint32_t)(x) >> 3) |
| 51 |
#endif |
| 52 |
|
| 53 |
typedef enum |
| 54 |
{ |
| 55 |
EXP50, |
| 56 |
EXP60, |
| 57 |
EXP70, |
| 58 |
EXP80, |
| 59 |
EXP90, |
| 60 |
EXP, |
| 61 |
LINEAR |
| 62 |
} Contrast; |
| 63 |
|
| 64 |
static uint32_t tnw = 216; /* thumbnail width */ |
| 65 |
static uint32_t tnh = 274; /* thumbnail height */ |
| 66 |
static Contrast contrast = LINEAR; /* current contrast */ |
| 67 |
static uint8_t *thumbnail; |
| 68 |
|
| 69 |
static int cpIFD(TIFF *, TIFF *); |
| 70 |
static int generateThumbnail(TIFF *, TIFF *); |
| 71 |
static void initScale(); |
| 72 |
static void usage(int code); |
| 73 |
|
| 74 |
#if !HAVE_DECL_OPTARG |
| 75 |
extern char *optarg; |
| 76 |
extern int optind; |
| 77 |
#endif |
| 78 |
|
| 79 |
int main(int argc, char *argv[]) |
| 80 |
{ |
| 81 |
TIFF *in; |
| 82 |
TIFF *out; |
| 83 |
int c; |
| 84 |
|
| 85 |
while ((c = getopt(argc, argv, "w:h:c:")) != -1) |
| 86 |
{ |
| 87 |
switch (c) |
| 88 |
{ |
| 89 |
case 'w': |
| 90 |
tnw = strtoul(optarg, NULL, 0); |
| 91 |
break; |
| 92 |
case 'h': |
| 93 |
tnh = strtoul(optarg, NULL, 0); |
| 94 |
break; |
| 95 |
case 'c': |
| 96 |
contrast = streq(optarg, "exp50") ? EXP50 |
| 97 |
: streq(optarg, "exp60") ? EXP60 |
| 98 |
: streq(optarg, "exp70") ? EXP70 |
| 99 |
: streq(optarg, "exp80") ? EXP80 |
| 100 |
: streq(optarg, "exp90") ? EXP90 |
| 101 |
: streq(optarg, "exp") ? EXP |
| 102 |
: streq(optarg, "linear") ? LINEAR |
| 103 |
: EXP; |
| 104 |
break; |
| 105 |
default: |
| 106 |
usage(EXIT_FAILURE); |
| 107 |
} |
| 108 |
} |
| 109 |
if (argc - optind != 2) |
| 110 |
usage(EXIT_FAILURE); |
| 111 |
|
| 112 |
out = TIFFOpen(argv[optind + 1], "w"); |
| 113 |
if (out == NULL) |
| 114 |
return 2; |
| 115 |
in = TIFFOpen(argv[optind], "r"); |
| 116 |
if (in == NULL) |
| 117 |
return 2; |
| 118 |
|
| 119 |
thumbnail = (uint8_t *)_TIFFmalloc(tnw * tnh); |
| 120 |
if (!thumbnail) |
| 121 |
{ |
| 122 |
TIFFError(TIFFFileName(in), |
| 123 |
"Can't allocate space for thumbnail buffer."); |
| 124 |
return EXIT_FAILURE; |
| 125 |
} |
| 126 |
|
| 127 |
if (in != NULL) |
| 128 |
{ |
| 129 |
initScale(); |
| 130 |
do |
| 131 |
{ |
| 132 |
if (!generateThumbnail(in, out)) |
| 133 |
goto bad; |
| 134 |
if (!cpIFD(in, out) || !TIFFWriteDirectory(out)) |
| 135 |
goto bad; |
| 136 |
} while (TIFFReadDirectory(in)); |
| 137 |
(void)TIFFClose(in); |
| 138 |
} |
| 139 |
(void)TIFFClose(out); |
| 140 |
return EXIT_SUCCESS; |
| 141 |
bad: |
| 142 |
(void)TIFFClose(out); |
| 143 |
return EXIT_FAILURE; |
| 144 |
} |
| 145 |
|
| 146 |
#define CopyField(tag, v) \ |
| 147 |
if (TIFFGetField(in, tag, &v)) \ |
| 148 |
TIFFSetField(out, tag, v) |
| 149 |
#define CopyField2(tag, v1, v2) \ |
| 150 |
if (TIFFGetField(in, tag, &v1, &v2)) \ |
| 151 |
TIFFSetField(out, tag, v1, v2) |
| 152 |
#define CopyField3(tag, v1, v2, v3) \ |
| 153 |
if (TIFFGetField(in, tag, &v1, &v2, &v3)) \ |
| 154 |
TIFFSetField(out, tag, v1, v2, v3) |
| 155 |
#define CopyField4(tag, v1, v2, v3, v4) \ |
| 156 |
if (TIFFGetField(in, tag, &v1, &v2, &v3, &v4)) \ |
| 157 |
TIFFSetField(out, tag, v1, v2, v3, v4) |
| 158 |
|
| 159 |
static void cpTag(TIFF *in, TIFF *out, uint16_t tag, uint16_t count, |
| 160 |
TIFFDataType type) |
| 161 |
{ |
| 162 |
switch (type) |
| 163 |
{ |
| 164 |
case TIFF_SHORT: |
| 165 |
if (count == 1) |
| 166 |
{ |
| 167 |
uint16_t shortv; |
| 168 |
CopyField(tag, shortv); |
| 169 |
} |
| 170 |
else if (count == 2) |
| 171 |
{ |
| 172 |
uint16_t shortv1, shortv2; |
| 173 |
CopyField2(tag, shortv1, shortv2); |
| 174 |
} |
| 175 |
else if (count == 4) |
| 176 |
{ |
| 177 |
uint16_t *tr, *tg, *tb, *ta; |
| 178 |
CopyField4(tag, tr, tg, tb, ta); |
| 179 |
} |
| 180 |
else if (count == (uint16_t)-1) |
| 181 |
{ |
| 182 |
uint16_t shortv1; |
| 183 |
uint16_t *shortav; |
| 184 |
CopyField2(tag, shortv1, shortav); |
| 185 |
} |
| 186 |
break; |
| 187 |
case TIFF_LONG: |
| 188 |
{ |
| 189 |
uint32_t longv; |
| 190 |
CopyField(tag, longv); |
| 191 |
} |
| 192 |
break; |
| 193 |
case TIFF_LONG8: |
| 194 |
{ |
| 195 |
uint64_t longv8; |
| 196 |
CopyField(tag, longv8); |
| 197 |
} |
| 198 |
break; |
| 199 |
case TIFF_SLONG8: |
| 200 |
{ |
| 201 |
int64_t longv8; |
| 202 |
CopyField(tag, longv8); |
| 203 |
} |
| 204 |
break; |
| 205 |
case TIFF_RATIONAL: |
| 206 |
if (count == 1) |
| 207 |
{ |
| 208 |
float floatv; |
| 209 |
CopyField(tag, floatv); |
| 210 |
} |
| 211 |
else if (count == (uint16_t)-1) |
| 212 |
{ |
| 213 |
float *floatav; |
| 214 |
CopyField(tag, floatav); |
| 215 |
} |
| 216 |
break; |
| 217 |
case TIFF_ASCII: |
| 218 |
{ |
| 219 |
char *stringv; |
| 220 |
CopyField(tag, stringv); |
| 221 |
} |
| 222 |
break; |
| 223 |
case TIFF_DOUBLE: |
| 224 |
if (count == 1) |
| 225 |
{ |
| 226 |
double doublev; |
| 227 |
CopyField(tag, doublev); |
| 228 |
} |
| 229 |
else if (count == (uint16_t)-1) |
| 230 |
{ |
| 231 |
double *doubleav; |
| 232 |
CopyField(tag, doubleav); |
| 233 |
} |
| 234 |
break; |
| 235 |
case TIFF_IFD8: |
| 236 |
{ |
| 237 |
toff_t ifd8; |
| 238 |
CopyField(tag, ifd8); |
| 239 |
} |
| 240 |
break; |
| 241 |
default: |
| 242 |
TIFFError(TIFFFileName(in), |
| 243 |
"Data type %d is not supported, tag %d skipped.", tag, |
| 244 |
type); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
#undef CopyField4 |
| 249 |
#undef CopyField3 |
| 250 |
#undef CopyField2 |
| 251 |
#undef CopyField |
| 252 |
|
| 253 |
static const struct cpTag |
| 254 |
{ |
| 255 |
uint16_t tag; |
| 256 |
uint16_t count; |
| 257 |
TIFFDataType type; |
| 258 |
} tags[] = { |
| 259 |
{TIFFTAG_IMAGEWIDTH, 1, TIFF_LONG}, |
| 260 |
{TIFFTAG_IMAGELENGTH, 1, TIFF_LONG}, |
| 261 |
{TIFFTAG_BITSPERSAMPLE, 1, TIFF_SHORT}, |
| 262 |
{TIFFTAG_COMPRESSION, 1, TIFF_SHORT}, |
| 263 |
{TIFFTAG_FILLORDER, 1, TIFF_SHORT}, |
| 264 |
{TIFFTAG_SAMPLESPERPIXEL, 1, TIFF_SHORT}, |
| 265 |
{TIFFTAG_ROWSPERSTRIP, 1, TIFF_LONG}, |
| 266 |
{TIFFTAG_PLANARCONFIG, 1, TIFF_SHORT}, |
| 267 |
{TIFFTAG_GROUP3OPTIONS, 1, TIFF_LONG}, |
| 268 |
{TIFFTAG_SUBFILETYPE, 1, TIFF_LONG}, |
| 269 |
{TIFFTAG_PHOTOMETRIC, 1, TIFF_SHORT}, |
| 270 |
{TIFFTAG_THRESHHOLDING, 1, TIFF_SHORT}, |
| 271 |
{TIFFTAG_DOCUMENTNAME, 1, TIFF_ASCII}, |
| 272 |
{TIFFTAG_IMAGEDESCRIPTION, 1, TIFF_ASCII}, |
| 273 |
{TIFFTAG_MAKE, 1, TIFF_ASCII}, |
| 274 |
{TIFFTAG_MODEL, 1, TIFF_ASCII}, |
| 275 |
{TIFFTAG_ORIENTATION, 1, TIFF_SHORT}, |
| 276 |
{TIFFTAG_MINSAMPLEVALUE, 1, TIFF_SHORT}, |
| 277 |
{TIFFTAG_MAXSAMPLEVALUE, 1, TIFF_SHORT}, |
| 278 |
{TIFFTAG_XRESOLUTION, 1, TIFF_RATIONAL}, |
| 279 |
{TIFFTAG_YRESOLUTION, 1, TIFF_RATIONAL}, |
| 280 |
{TIFFTAG_PAGENAME, 1, TIFF_ASCII}, |
| 281 |
{TIFFTAG_XPOSITION, 1, TIFF_RATIONAL}, |
| 282 |
{TIFFTAG_YPOSITION, 1, TIFF_RATIONAL}, |
| 283 |
{TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG}, |
| 284 |
{TIFFTAG_RESOLUTIONUNIT, 1, TIFF_SHORT}, |
| 285 |
{TIFFTAG_PAGENUMBER, 2, TIFF_SHORT}, |
| 286 |
{TIFFTAG_SOFTWARE, 1, TIFF_ASCII}, |
| 287 |
{TIFFTAG_DATETIME, 1, TIFF_ASCII}, |
| 288 |
{TIFFTAG_ARTIST, 1, TIFF_ASCII}, |
| 289 |
{TIFFTAG_HOSTCOMPUTER, 1, TIFF_ASCII}, |
| 290 |
{TIFFTAG_WHITEPOINT, 2, TIFF_RATIONAL}, |
| 291 |
{TIFFTAG_PRIMARYCHROMATICITIES, (uint16_t)-1, TIFF_RATIONAL}, |
| 292 |
{TIFFTAG_HALFTONEHINTS, 2, TIFF_SHORT}, |
| 293 |
{TIFFTAG_BADFAXLINES, 1, TIFF_LONG}, |
| 294 |
{TIFFTAG_CLEANFAXDATA, 1, TIFF_SHORT}, |
| 295 |
{TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG}, |
| 296 |
{TIFFTAG_INKSET, 1, TIFF_SHORT}, |
| 297 |
/*{ TIFFTAG_INKNAMES, 1, TIFF_ASCII },*/ /* Needs much |
| 298 |
more |
| 299 |
complicated |
| 300 |
logic. See |
| 301 |
tiffcp */ |
| 302 |
{TIFFTAG_DOTRANGE, 2, TIFF_SHORT}, |
| 303 |
{TIFFTAG_TARGETPRINTER, 1, TIFF_ASCII}, |
| 304 |
{TIFFTAG_SAMPLEFORMAT, 1, TIFF_SHORT}, |
| 305 |
{TIFFTAG_YCBCRCOEFFICIENTS, (uint16_t)-1, TIFF_RATIONAL}, |
| 306 |
{TIFFTAG_YCBCRSUBSAMPLING, 2, TIFF_SHORT}, |
| 307 |
{TIFFTAG_YCBCRPOSITIONING, 1, TIFF_SHORT}, |
| 308 |
{TIFFTAG_REFERENCEBLACKWHITE, (uint16_t)-1, TIFF_RATIONAL}, |
| 309 |
{TIFFTAG_EXTRASAMPLES, (uint16_t)-1, TIFF_SHORT}, |
| 310 |
}; |
| 311 |
#define NTAGS (sizeof(tags) / sizeof(tags[0])) |
| 312 |
|
| 313 |
static void cpTags(TIFF *in, TIFF *out) |
| 314 |
{ |
| 315 |
const struct cpTag *p; |
| 316 |
for (p = tags; p < &tags[NTAGS]; p++) |
| 317 |
{ |
| 318 |
/* Horrible: but TIFFGetField() expects 2 arguments to be passed */ |
| 319 |
/* if we request a tag that is defined in a codec, but that codec */ |
| 320 |
/* isn't used */ |
| 321 |
if (p->tag == TIFFTAG_GROUP3OPTIONS) |
| 322 |
{ |
| 323 |
uint16_t compression; |
| 324 |
if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) || |
| 325 |
compression != COMPRESSION_CCITTFAX3) |
| 326 |
continue; |
| 327 |
} |
| 328 |
if (p->tag == TIFFTAG_GROUP4OPTIONS) |
| 329 |
{ |
| 330 |
uint16_t compression; |
| 331 |
if (!TIFFGetField(in, TIFFTAG_COMPRESSION, &compression) || |
| 332 |
compression != COMPRESSION_CCITTFAX4) |
| 333 |
continue; |
| 334 |
} |
| 335 |
cpTag(in, out, p->tag, p->count, p->type); |
| 336 |
} |
| 337 |
} |
| 338 |
#undef NTAGS |
| 339 |
|
| 340 |
static int cpStrips(TIFF *in, TIFF *out) |
| 341 |
{ |
| 342 |
tsize_t bufsize = TIFFStripSize(in); |
| 343 |
unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); |
| 344 |
|
| 345 |
if (buf) |
| 346 |
{ |
| 347 |
tstrip_t s, ns = TIFFNumberOfStrips(in); |
| 348 |
uint64_t *bytecounts; |
| 349 |
|
| 350 |
TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts); |
| 351 |
for (s = 0; s < ns; s++) |
| 352 |
{ |
| 353 |
if (bytecounts[s] > (uint64_t)bufsize) |
| 354 |
{ |
| 355 |
buf = |
| 356 |
(unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]); |
| 357 |
if (!buf) |
| 358 |
goto bad; |
| 359 |
bufsize = (tmsize_t)bytecounts[s]; |
| 360 |
} |
| 361 |
if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 || |
| 362 |
TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) |
| 363 |
{ |
| 364 |
_TIFFfree(buf); |
| 365 |
return 0; |
| 366 |
} |
| 367 |
} |
| 368 |
_TIFFfree(buf); |
| 369 |
return 1; |
| 370 |
} |
| 371 |
|
| 372 |
bad: |
| 373 |
TIFFError(TIFFFileName(in), "Can't allocate space for strip buffer."); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
static int cpTiles(TIFF *in, TIFF *out) |
| 378 |
{ |
| 379 |
tsize_t bufsize = TIFFTileSize(in); |
| 380 |
unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize); |
| 381 |
|
| 382 |
if (buf) |
| 383 |
{ |
| 384 |
ttile_t t, nt = TIFFNumberOfTiles(in); |
| 385 |
uint64_t *bytecounts; |
| 386 |
|
| 387 |
TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts); |
| 388 |
for (t = 0; t < nt; t++) |
| 389 |
{ |
| 390 |
if (bytecounts[t] > (uint64_t)bufsize) |
| 391 |
{ |
| 392 |
buf = |
| 393 |
(unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[t]); |
| 394 |
if (!buf) |
| 395 |
goto bad; |
| 396 |
bufsize = (tmsize_t)bytecounts[t]; |
| 397 |
} |
| 398 |
if (TIFFReadRawTile(in, t, buf, (tmsize_t)bytecounts[t]) < 0 || |
| 399 |
TIFFWriteRawTile(out, t, buf, (tmsize_t)bytecounts[t]) < 0) |
| 400 |
{ |
| 401 |
_TIFFfree(buf); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
} |
| 405 |
_TIFFfree(buf); |
| 406 |
return 1; |
| 407 |
} |
| 408 |
|
| 409 |
bad: |
| 410 |
TIFFError(TIFFFileName(in), "Can't allocate space for tile buffer."); |
| 411 |
return (0); |
| 412 |
} |
| 413 |
|
| 414 |
static int cpIFD(TIFF *in, TIFF *out) |
| 415 |
{ |
| 416 |
cpTags(in, out); |
| 417 |
if (TIFFIsTiled(in)) |
| 418 |
{ |
| 419 |
if (!cpTiles(in, out)) |
| 420 |
return (0); |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
if (!cpStrips(in, out)) |
| 425 |
return (0); |
| 426 |
} |
| 427 |
return (1); |
| 428 |
} |
| 429 |
|
| 430 |
static uint16_t photometric; /* current photometric of raster */ |
| 431 |
static uint16_t filterWidth; /* filter width in pixels */ |
| 432 |
static uint32_t stepSrcWidth; /* src image stepping width */ |
| 433 |
static uint32_t stepDstWidth; /* dest stepping width */ |
| 434 |
static uint8_t *src0; /* horizontal bit stepping (start) */ |
| 435 |
static uint8_t *src1; /* horizontal bit stepping (middle) */ |
| 436 |
static uint8_t *src2; /* horizontal bit stepping (end) */ |
| 437 |
static uint32_t *rowoff; /* row offset for stepping */ |
| 438 |
static uint8_t cmap[256]; /* colormap indexes */ |
| 439 |
static uint8_t bits[256]; /* count of bits set */ |
| 440 |
|
| 441 |
static void setupBitsTables() |
| 442 |
{ |
| 443 |
int i; |
| 444 |
for (i = 0; i < 256; i++) |
| 445 |
{ |
| 446 |
int n = 0; |
| 447 |
if (i & 0x01) |
| 448 |
n++; |
| 449 |
if (i & 0x02) |
| 450 |
n++; |
| 451 |
if (i & 0x04) |
| 452 |
n++; |
| 453 |
if (i & 0x08) |
| 454 |
n++; |
| 455 |
if (i & 0x10) |
| 456 |
n++; |
| 457 |
if (i & 0x20) |
| 458 |
n++; |
| 459 |
if (i & 0x40) |
| 460 |
n++; |
| 461 |
if (i & 0x80) |
| 462 |
n++; |
| 463 |
bits[i] = n; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
static int clamp(float v, int low, int high) |
| 468 |
{ |
| 469 |
return (v < low ? low : v > high ? high : (int)v); |
| 470 |
} |
| 471 |
|
| 472 |
#ifndef M_E |
| 473 |
#define M_E 2.7182818284590452354 |
| 474 |
#endif |
| 475 |
|
| 476 |
static void expFill(float pct[], uint32_t p, uint32_t n) |
| 477 |
{ |
| 478 |
uint32_t i; |
| 479 |
uint32_t c = (p * n) / 100; |
| 480 |
for (i = 1; i < c; i++) |
| 481 |
pct[i] = (float)(1 - exp(i / ((double)(n - 1))) / M_E); |
| 482 |
for (; i < n; i++) |
| 483 |
pct[i] = 0.; |
| 484 |
} |
| 485 |
|
| 486 |
static void setupCmap() |
| 487 |
{ |
| 488 |
float pct[256]; /* known to be large enough */ |
| 489 |
uint32_t i; |
| 490 |
pct[0] = 1; /* force white */ |
| 491 |
switch (contrast) |
| 492 |
{ |
| 493 |
case EXP50: |
| 494 |
expFill(pct, 50, 256); |
| 495 |
break; |
| 496 |
case EXP60: |
| 497 |
expFill(pct, 60, 256); |
| 498 |
break; |
| 499 |
case EXP70: |
| 500 |
expFill(pct, 70, 256); |
| 501 |
break; |
| 502 |
case EXP80: |
| 503 |
expFill(pct, 80, 256); |
| 504 |
break; |
| 505 |
case EXP90: |
| 506 |
expFill(pct, 90, 256); |
| 507 |
break; |
| 508 |
case EXP: |
| 509 |
expFill(pct, 100, 256); |
| 510 |
break; |
| 511 |
case LINEAR: |
| 512 |
for (i = 1; i < 256; i++) |
| 513 |
pct[i] = 1 - ((float)i) / (256 - 1); |
| 514 |
break; |
| 515 |
} |
| 516 |
switch (photometric) |
| 517 |
{ |
| 518 |
case PHOTOMETRIC_MINISWHITE: |
| 519 |
for (i = 0; i < 256; i++) |
| 520 |
cmap[i] = clamp(255 * pct[(256 - 1) - i], 0, 255); |
| 521 |
break; |
| 522 |
case PHOTOMETRIC_MINISBLACK: |
| 523 |
for (i = 0; i < 256; i++) |
| 524 |
cmap[i] = clamp(255 * pct[i], 0, 255); |
| 525 |
break; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
static void initScale() |
| 530 |
{ |
| 531 |
src0 = (uint8_t *)_TIFFmalloc(sizeof(uint8_t) * tnw); |
| 532 |
src1 = (uint8_t *)_TIFFmalloc(sizeof(uint8_t) * tnw); |
| 533 |
src2 = (uint8_t *)_TIFFmalloc(sizeof(uint8_t) * tnw); |
| 534 |
rowoff = (uint32_t *)_TIFFmalloc(sizeof(uint32_t) * tnw); |
| 535 |
filterWidth = 0; |
| 536 |
stepDstWidth = stepSrcWidth = 0; |
| 537 |
setupBitsTables(); |
| 538 |
} |
| 539 |
|
| 540 |
/* |
| 541 |
* Calculate the horizontal accumulation parameteres |
| 542 |
* according to the widths of the src and dst images. |
| 543 |
*/ |
| 544 |
static void setupStepTables(uint32_t sw) |
| 545 |
{ |
| 546 |
if (stepSrcWidth != sw || stepDstWidth != tnw) |
| 547 |
{ |
| 548 |
int step = sw; |
| 549 |
int limit = tnw; |
| 550 |
int err = 0; |
| 551 |
uint32_t sx = 0; |
| 552 |
uint32_t x; |
| 553 |
int fw; |
| 554 |
uint8_t b; |
| 555 |
for (x = 0; x < tnw; x++) |
| 556 |
{ |
| 557 |
uint32_t sx0 = sx; |
| 558 |
err += step; |
| 559 |
while (err >= limit) |
| 560 |
{ |
| 561 |
err -= limit; |
| 562 |
sx++; |
| 563 |
} |
| 564 |
rowoff[x] = sx0 >> 3; |
| 565 |
fw = sx - sx0; /* width */ |
| 566 |
b = (fw < 8) ? (uint8_t)(0xff << (8 - fw)) : (uint8_t)0xff; |
| 567 |
src0[x] = b >> (sx0 & 7); |
| 568 |
fw -= 8 - (sx0 & 7); |
| 569 |
if (fw < 0) |
| 570 |
fw = 0; |
| 571 |
src1[x] = fw >> 3; |
| 572 |
fw -= (fw >> 3) << 3; |
| 573 |
src2[x] = 0xff << (8 - fw); |
| 574 |
} |
| 575 |
stepSrcWidth = sw; |
| 576 |
stepDstWidth = tnw; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
static void setrow(uint8_t *row, uint32_t nrows, const uint8_t *rows[]) |
| 581 |
{ |
| 582 |
uint32_t x; |
| 583 |
uint32_t area = nrows * filterWidth; |
| 584 |
for (x = 0; x < tnw; x++) |
| 585 |
{ |
| 586 |
uint32_t mask0 = src0[x]; |
| 587 |
uint32_t fw = src1[x]; |
| 588 |
uint32_t mask1 = src1[x]; |
| 589 |
uint32_t off = rowoff[x]; |
| 590 |
uint32_t acc = 0; |
| 591 |
uint32_t y, i; |
| 592 |
for (y = 0; y < nrows; y++) |
| 593 |
{ |
| 594 |
const uint8_t *src = rows[y] + off; |
| 595 |
acc += bits[*src++ & mask0]; |
| 596 |
switch (fw) |
| 597 |
{ |
| 598 |
default: |
| 599 |
for (i = fw; i > 8; i--) |
| 600 |
acc += bits[*src++]; |
| 601 |
/* fall through... */ |
| 602 |
case 8: |
| 603 |
acc += bits[*src++]; /* fall through */ |
| 604 |
case 7: |
| 605 |
acc += bits[*src++]; /* fall through */ |
| 606 |
case 6: |
| 607 |
acc += bits[*src++]; /* fall through */ |
| 608 |
case 5: |
| 609 |
acc += bits[*src++]; /* fall through */ |
| 610 |
case 4: |
| 611 |
acc += bits[*src++]; /* fall through */ |
| 612 |
case 3: |
| 613 |
acc += bits[*src++]; /* fall through */ |
| 614 |
case 2: |
| 615 |
acc += bits[*src++]; /* fall through */ |
| 616 |
case 1: |
| 617 |
acc += bits[*src++]; /* fall through */ |
| 618 |
case 0: |
| 619 |
break; |
| 620 |
} |
| 621 |
acc += bits[*src & mask1]; |
| 622 |
} |
| 623 |
*row++ = cmap[(255 * acc) / area]; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
/* |
| 628 |
* Install the specified image. The |
| 629 |
* image is resized to fit the display page using |
| 630 |
* a box filter. The resultant pixels are mapped |
| 631 |
* with a user-selectable contrast curve. |
| 632 |
*/ |
| 633 |
static void setImage1(const uint8_t *br, uint32_t rw, uint32_t rh) |
| 634 |
{ |
| 635 |
int step = rh; |
| 636 |
int limit = tnh; |
| 637 |
int err = 0; |
| 638 |
int bpr = TIFFhowmany8(rw); |
| 639 |
int sy = 0; |
| 640 |
uint8_t *row = thumbnail; |
| 641 |
uint32_t dy; |
| 642 |
for (dy = 0; dy < tnh; dy++) |
| 643 |
{ |
| 644 |
const uint8_t *rows[256]; |
| 645 |
uint32_t nrows = 1; |
| 646 |
fprintf(stderr, "bpr=%d, sy=%d, bpr*sy=%d\n", bpr, sy, bpr * sy); |
| 647 |
rows[0] = br + bpr * sy; |
| 648 |
err += step; |
| 649 |
while (err >= limit) |
| 650 |
{ |
| 651 |
err -= limit; |
| 652 |
sy++; |
| 653 |
if (err >= limit) |
| 654 |
{ |
| 655 |
/* We should perhaps error loudly, but I can't make sense of |
| 656 |
* that */ |
| 657 |
/* code... */ |
| 658 |
if (nrows == 256) |
| 659 |
break; |
| 660 |
rows[nrows++] = br + bpr * sy; |
| 661 |
} |
| 662 |
} |
| 663 |
setrow(row, nrows, rows); |
| 664 |
row += tnw; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
static void setImage(const uint8_t *br, uint32_t rw, uint32_t rh) |
| 669 |
{ |
| 670 |
filterWidth = (uint16_t)ceil((double)rw / (double)tnw); |
| 671 |
setupStepTables(rw); |
| 672 |
setImage1(br, rw, rh); |
| 673 |
} |
| 674 |
|
| 675 |
static int generateThumbnail(TIFF *in, TIFF *out) |
| 676 |
{ |
| 677 |
unsigned char *raster; |
| 678 |
unsigned char *rp; |
| 679 |
uint32_t sw, sh, rps; |
| 680 |
uint16_t bps, spp; |
| 681 |
tsize_t rowsize, rastersize; |
| 682 |
tstrip_t s, ns = TIFFNumberOfStrips(in); |
| 683 |
toff_t diroff[1]; |
| 684 |
|
| 685 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &sw); |
| 686 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &sh); |
| 687 |
TIFFGetFieldDefaulted(in, TIFFTAG_BITSPERSAMPLE, &bps); |
| 688 |
TIFFGetFieldDefaulted(in, TIFFTAG_SAMPLESPERPIXEL, &spp); |
| 689 |
TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &rps); |
| 690 |
if (spp != 1 || bps != 1) |
| 691 |
return 0; |
| 692 |
rowsize = TIFFScanlineSize(in); |
| 693 |
rastersize = sh * rowsize; |
| 694 |
fprintf(stderr, "rastersize=%u\n", (unsigned int)rastersize); |
| 695 |
/* +3 : add a few guard bytes since setrow() can read a bit */ |
| 696 |
/* outside buffer */ |
| 697 |
raster = (unsigned char *)_TIFFmalloc(rastersize + 3); |
| 698 |
if (!raster) |
| 699 |
{ |
| 700 |
TIFFError(TIFFFileName(in), "Can't allocate space for raster buffer."); |
| 701 |
return 0; |
| 702 |
} |
| 703 |
raster[rastersize] = 0; |
| 704 |
raster[rastersize + 1] = 0; |
| 705 |
raster[rastersize + 2] = 0; |
| 706 |
rp = raster; |
| 707 |
for (s = 0; s < ns; s++) |
| 708 |
{ |
| 709 |
(void)TIFFReadEncodedStrip(in, s, rp, -1); |
| 710 |
rp += rps * rowsize; |
| 711 |
} |
| 712 |
TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photometric); |
| 713 |
setupCmap(); |
| 714 |
setImage(raster, sw, sh); |
| 715 |
_TIFFfree(raster); |
| 716 |
|
| 717 |
TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE); |
| 718 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32_t)tnw); |
| 719 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32_t)tnh); |
| 720 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, (uint16_t)8); |
| 721 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, (uint16_t)1); |
| 722 |
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS); |
| 723 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); |
| 724 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 725 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 726 |
cpTag(in, out, TIFFTAG_SOFTWARE, (uint16_t)-1, TIFF_ASCII); |
| 727 |
cpTag(in, out, TIFFTAG_IMAGEDESCRIPTION, (uint16_t)-1, TIFF_ASCII); |
| 728 |
cpTag(in, out, TIFFTAG_DATETIME, (uint16_t)-1, TIFF_ASCII); |
| 729 |
cpTag(in, out, TIFFTAG_HOSTCOMPUTER, (uint16_t)-1, TIFF_ASCII); |
| 730 |
diroff[0] = 0UL; |
| 731 |
TIFFSetField(out, TIFFTAG_SUBIFD, 1, diroff); |
| 732 |
return (TIFFWriteEncodedStrip(out, 0, thumbnail, tnw * tnh) != -1 && |
| 733 |
TIFFWriteDirectory(out) != -1); |
| 734 |
} |
| 735 |
|
| 736 |
const char *usage_info[] = { |
| 737 |
"Create a TIFF file with thumbnail images\n\n" |
| 738 |
"usage: thumbnail [options] input.tif output.tif", |
| 739 |
"where options are:", |
| 740 |
" -h # specify thumbnail image height (default is 274)", |
| 741 |
" -w # specify thumbnail image width (default is 216)", |
| 742 |
"", |
| 743 |
" -c linear use linear contrast curve", |
| 744 |
" -c exp50 use 50% exponential contrast curve", |
| 745 |
" -c exp60 use 60% exponential contrast curve", |
| 746 |
" -c exp70 use 70% exponential contrast curve", |
| 747 |
" -c exp80 use 80% exponential contrast curve", |
| 748 |
" -c exp90 use 90% exponential contrast curve", |
| 749 |
" -c exp use pure exponential contrast curve", |
| 750 |
NULL}; |
| 751 |
|
| 752 |
static void usage(int code) |
| 753 |
{ |
| 754 |
int i; |
| 755 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 756 |
|
| 757 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 758 |
for (i = 0; usage_info[i] != NULL; i++) |
| 759 |
fprintf(out, "%s\n", usage_info[i]); |
| 760 |
exit(code); |
| 761 |
} |