| 1 |
/* |
| 2 |
* Project: libtiff tools |
| 3 |
* Purpose: Convert raw byte sequences in TIFF images |
| 4 |
* Author: Andrey Kiselev, [email protected] |
| 5 |
* |
| 6 |
****************************************************************************** |
| 7 |
* Copyright (c) 2002, Andrey Kiselev <[email protected]> |
| 8 |
* |
| 9 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 10 |
* its documentation for any purpose is hereby granted without fee, provided |
| 11 |
* that (i) the above copyright notices and this permission notice appear in |
| 12 |
* all copies of the software and related documentation, and (ii) the names of |
| 13 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 14 |
* publicity relating to the software without the specific, prior written |
| 15 |
* permission of Sam Leffler and Silicon Graphics. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 19 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 20 |
* |
| 21 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 22 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 23 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 24 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 25 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 26 |
* OF THIS SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include "libport.h" |
| 30 |
#include "tif_config.h" |
| 31 |
|
| 32 |
#include <ctype.h> |
| 33 |
#include <math.h> |
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
#include <sys/stat.h> |
| 38 |
#include <sys/types.h> |
| 39 |
|
| 40 |
#ifdef HAVE_UNISTD_H |
| 41 |
#include <unistd.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#if HAVE_FCNTL_H |
| 45 |
#include <fcntl.h> |
| 46 |
#endif |
| 47 |
|
| 48 |
#if HAVE_SYS_TYPES_H |
| 49 |
#include <sys/types.h> |
| 50 |
#endif |
| 51 |
|
| 52 |
#if HAVE_IO_H |
| 53 |
#include <io.h> |
| 54 |
#endif |
| 55 |
|
| 56 |
#include "tiffio.h" |
| 57 |
#include "tiffiop.h" |
| 58 |
|
| 59 |
#ifndef EXIT_SUCCESS |
| 60 |
#define EXIT_SUCCESS 0 |
| 61 |
#endif |
| 62 |
#ifndef EXIT_FAILURE |
| 63 |
#define EXIT_FAILURE 1 |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifndef O_BINARY |
| 67 |
#define O_BINARY 0 |
| 68 |
#endif |
| 69 |
|
| 70 |
typedef enum |
| 71 |
{ |
| 72 |
PIXEL, |
| 73 |
BAND |
| 74 |
} InterleavingType; |
| 75 |
|
| 76 |
static uint16_t compression = (uint16_t)-1; |
| 77 |
static int jpegcolormode = JPEGCOLORMODE_RGB; |
| 78 |
static int quality = 75; /* JPEG quality */ |
| 79 |
static uint16_t predictor = 0; |
| 80 |
|
| 81 |
static void swapBytesInScanline(void *, uint32_t, TIFFDataType); |
| 82 |
static int guessSize(int, TIFFDataType, _TIFF_off_t, uint32_t, int, uint32_t *, |
| 83 |
uint32_t *); |
| 84 |
static double correlation(void *, void *, uint32_t, TIFFDataType); |
| 85 |
static void usage(int); |
| 86 |
static int processCompressOptions(char *); |
| 87 |
|
| 88 |
int main(int argc, char *argv[]) |
| 89 |
{ |
| 90 |
uint32_t width = 0, length = 0, linebytes, bufsize; |
| 91 |
uint32_t nbands = 1; /* number of bands in input image*/ |
| 92 |
_TIFF_off_t hdr_size = 0; /* size of the header to skip */ |
| 93 |
TIFFDataType dtype = TIFF_BYTE; |
| 94 |
int16_t depth = 1; /* bytes per pixel in input image */ |
| 95 |
int swab = 0; /* byte swapping flag */ |
| 96 |
InterleavingType interleaving = 0; /* interleaving type flag */ |
| 97 |
uint32_t rowsperstrip = (uint32_t)-1; |
| 98 |
uint16_t photometric = PHOTOMETRIC_MINISBLACK; |
| 99 |
uint16_t config = PLANARCONFIG_CONTIG; |
| 100 |
uint16_t fillorder = FILLORDER_LSB2MSB; |
| 101 |
int fd; |
| 102 |
char *outfilename = NULL; |
| 103 |
TIFF *out; |
| 104 |
uint32_t temp_limit_check = 0; /* temp for integer overflow checking*/ |
| 105 |
|
| 106 |
uint32_t row, col, band; |
| 107 |
int c; |
| 108 |
unsigned char *buf = NULL, *buf1 = NULL; |
| 109 |
#if !HAVE_DECL_OPTARG |
| 110 |
extern int optind; |
| 111 |
extern char *optarg; |
| 112 |
#endif |
| 113 |
|
| 114 |
while ((c = getopt(argc, argv, "c:r:H:w:l:b:d:LMp:si:o:h")) != -1) |
| 115 |
{ |
| 116 |
switch (c) |
| 117 |
{ |
| 118 |
case 'c': /* compression scheme */ |
| 119 |
if (!processCompressOptions(optarg)) |
| 120 |
usage(EXIT_FAILURE); |
| 121 |
break; |
| 122 |
case 'r': /* rows/strip */ |
| 123 |
rowsperstrip = atoi(optarg); |
| 124 |
break; |
| 125 |
case 'H': /* size of input image file header */ |
| 126 |
hdr_size = atoi(optarg); |
| 127 |
break; |
| 128 |
case 'w': /* input image width */ |
| 129 |
width = atoi(optarg); |
| 130 |
break; |
| 131 |
case 'l': /* input image length */ |
| 132 |
length = atoi(optarg); |
| 133 |
break; |
| 134 |
case 'b': /* number of bands in input image */ |
| 135 |
nbands = atoi(optarg); |
| 136 |
break; |
| 137 |
case 'd': /* type of samples in input image */ |
| 138 |
if (strncmp(optarg, "byte", 4) == 0) |
| 139 |
dtype = TIFF_BYTE; |
| 140 |
else if (strncmp(optarg, "short", 5) == 0) |
| 141 |
dtype = TIFF_SHORT; |
| 142 |
else if (strncmp(optarg, "long", 4) == 0) |
| 143 |
dtype = TIFF_LONG; |
| 144 |
else if (strncmp(optarg, "sbyte", 5) == 0) |
| 145 |
dtype = TIFF_SBYTE; |
| 146 |
else if (strncmp(optarg, "sshort", 6) == 0) |
| 147 |
dtype = TIFF_SSHORT; |
| 148 |
else if (strncmp(optarg, "slong", 5) == 0) |
| 149 |
dtype = TIFF_SLONG; |
| 150 |
else if (strncmp(optarg, "float", 5) == 0) |
| 151 |
dtype = TIFF_FLOAT; |
| 152 |
else if (strncmp(optarg, "double", 6) == 0) |
| 153 |
dtype = TIFF_DOUBLE; |
| 154 |
else |
| 155 |
dtype = TIFF_BYTE; |
| 156 |
depth = TIFFDataWidth(dtype); |
| 157 |
break; |
| 158 |
case 'L': /* input has lsb-to-msb fillorder */ |
| 159 |
fillorder = FILLORDER_LSB2MSB; |
| 160 |
break; |
| 161 |
case 'M': /* input has msb-to-lsb fillorder */ |
| 162 |
fillorder = FILLORDER_MSB2LSB; |
| 163 |
break; |
| 164 |
case 'p': /* photometric interpretation */ |
| 165 |
if (strncmp(optarg, "miniswhite", 10) == 0) |
| 166 |
photometric = PHOTOMETRIC_MINISWHITE; |
| 167 |
else if (strncmp(optarg, "minisblack", 10) == 0) |
| 168 |
photometric = PHOTOMETRIC_MINISBLACK; |
| 169 |
else if (strncmp(optarg, "rgb", 3) == 0) |
| 170 |
photometric = PHOTOMETRIC_RGB; |
| 171 |
else if (strncmp(optarg, "cmyk", 4) == 0) |
| 172 |
photometric = PHOTOMETRIC_SEPARATED; |
| 173 |
else if (strncmp(optarg, "ycbcr", 5) == 0) |
| 174 |
photometric = PHOTOMETRIC_YCBCR; |
| 175 |
else if (strncmp(optarg, "cielab", 6) == 0) |
| 176 |
photometric = PHOTOMETRIC_CIELAB; |
| 177 |
else if (strncmp(optarg, "icclab", 6) == 0) |
| 178 |
photometric = PHOTOMETRIC_ICCLAB; |
| 179 |
else if (strncmp(optarg, "itulab", 6) == 0) |
| 180 |
photometric = PHOTOMETRIC_ITULAB; |
| 181 |
else |
| 182 |
photometric = PHOTOMETRIC_MINISBLACK; |
| 183 |
break; |
| 184 |
case 's': /* do we need to swap bytes? */ |
| 185 |
swab = 1; |
| 186 |
break; |
| 187 |
case 'i': /* type of interleaving */ |
| 188 |
if (strncmp(optarg, "pixel", 4) == 0) |
| 189 |
interleaving = PIXEL; |
| 190 |
else if (strncmp(optarg, "band", 6) == 0) |
| 191 |
interleaving = BAND; |
| 192 |
else |
| 193 |
interleaving = 0; |
| 194 |
break; |
| 195 |
case 'o': |
| 196 |
outfilename = optarg; |
| 197 |
break; |
| 198 |
case 'h': |
| 199 |
usage(EXIT_SUCCESS); |
| 200 |
default: |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
if (argc - optind < 2) |
| 206 |
usage(EXIT_FAILURE); |
| 207 |
|
| 208 |
fd = open(argv[optind], O_RDONLY | O_BINARY, 0); |
| 209 |
if (fd < 0) |
| 210 |
{ |
| 211 |
fprintf(stderr, "%s: %s: Cannot open input file.\n", argv[0], |
| 212 |
argv[optind]); |
| 213 |
return (EXIT_FAILURE); |
| 214 |
} |
| 215 |
|
| 216 |
if (nbands == 0) |
| 217 |
{ |
| 218 |
fprintf(stderr, "The number of bands is illegal.\n"); |
| 219 |
return (-1); |
| 220 |
} |
| 221 |
|
| 222 |
if (guessSize(fd, dtype, hdr_size, nbands, swab, &width, &length) < 0) |
| 223 |
return EXIT_FAILURE; |
| 224 |
|
| 225 |
/* check for integer overflow in */ |
| 226 |
/* hdr_size + (*width) * (*length) * nbands * depth */ |
| 227 |
|
| 228 |
if ((width == 0) || (length == 0)) |
| 229 |
{ |
| 230 |
fprintf(stderr, "Too large nbands value specified.\n"); |
| 231 |
return (EXIT_FAILURE); |
| 232 |
} |
| 233 |
|
| 234 |
temp_limit_check = nbands * depth; |
| 235 |
|
| 236 |
if (!temp_limit_check || length > (UINT_MAX / temp_limit_check)) |
| 237 |
{ |
| 238 |
fprintf(stderr, "Too large length size specified.\n"); |
| 239 |
return (EXIT_FAILURE); |
| 240 |
} |
| 241 |
temp_limit_check = temp_limit_check * length; |
| 242 |
|
| 243 |
if (!temp_limit_check || width > (UINT_MAX / temp_limit_check)) |
| 244 |
{ |
| 245 |
fprintf(stderr, "Too large width size specified.\n"); |
| 246 |
return (EXIT_FAILURE); |
| 247 |
} |
| 248 |
temp_limit_check = temp_limit_check * width; |
| 249 |
|
| 250 |
if (!temp_limit_check || hdr_size > (UINT_MAX - temp_limit_check)) |
| 251 |
{ |
| 252 |
fprintf(stderr, "Too large header size specified.\n"); |
| 253 |
return (EXIT_FAILURE); |
| 254 |
} |
| 255 |
|
| 256 |
if (outfilename == NULL) |
| 257 |
outfilename = argv[optind + 1]; |
| 258 |
out = TIFFOpen(outfilename, "w"); |
| 259 |
if (out == NULL) |
| 260 |
{ |
| 261 |
fprintf(stderr, "%s: %s: Cannot open file for output.\n", argv[0], |
| 262 |
outfilename); |
| 263 |
return (EXIT_FAILURE); |
| 264 |
} |
| 265 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); |
| 266 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, length); |
| 267 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 268 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nbands); |
| 269 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, depth * 8); |
| 270 |
TIFFSetField(out, TIFFTAG_FILLORDER, fillorder); |
| 271 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, config); |
| 272 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photometric); |
| 273 |
switch (dtype) |
| 274 |
{ |
| 275 |
case TIFF_BYTE: |
| 276 |
case TIFF_SHORT: |
| 277 |
case TIFF_LONG: |
| 278 |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); |
| 279 |
break; |
| 280 |
case TIFF_SBYTE: |
| 281 |
case TIFF_SSHORT: |
| 282 |
case TIFF_SLONG: |
| 283 |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT); |
| 284 |
break; |
| 285 |
case TIFF_FLOAT: |
| 286 |
case TIFF_DOUBLE: |
| 287 |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP); |
| 288 |
break; |
| 289 |
default: |
| 290 |
TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_VOID); |
| 291 |
break; |
| 292 |
} |
| 293 |
if (compression == (uint16_t)-1) |
| 294 |
compression = COMPRESSION_PACKBITS; |
| 295 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 296 |
switch (compression) |
| 297 |
{ |
| 298 |
case COMPRESSION_JPEG: |
| 299 |
if (photometric == PHOTOMETRIC_RGB && |
| 300 |
jpegcolormode == JPEGCOLORMODE_RGB) |
| 301 |
photometric = PHOTOMETRIC_YCBCR; |
| 302 |
TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality); |
| 303 |
TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, jpegcolormode); |
| 304 |
break; |
| 305 |
case COMPRESSION_LZW: |
| 306 |
case COMPRESSION_ADOBE_DEFLATE: |
| 307 |
case COMPRESSION_DEFLATE: |
| 308 |
if (predictor != 0) |
| 309 |
TIFFSetField(out, TIFFTAG_PREDICTOR, predictor); |
| 310 |
break; |
| 311 |
} |
| 312 |
switch (interleaving) |
| 313 |
{ |
| 314 |
case BAND: /* band interleaved data */ |
| 315 |
linebytes = width * depth; |
| 316 |
buf = (unsigned char *)_TIFFmalloc(linebytes); |
| 317 |
break; |
| 318 |
case PIXEL: /* pixel interleaved data */ |
| 319 |
default: |
| 320 |
linebytes = width * nbands * depth; |
| 321 |
break; |
| 322 |
} |
| 323 |
bufsize = width * nbands * depth; |
| 324 |
buf1 = (unsigned char *)_TIFFmalloc(bufsize); |
| 325 |
|
| 326 |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); |
| 327 |
if (rowsperstrip > length) |
| 328 |
{ |
| 329 |
rowsperstrip = length; |
| 330 |
} |
| 331 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 332 |
|
| 333 |
_TIFF_lseek_f(fd, hdr_size, SEEK_SET); /* Skip the file header */ |
| 334 |
for (row = 0; row < length; row++) |
| 335 |
{ |
| 336 |
switch (interleaving) |
| 337 |
{ |
| 338 |
case BAND: /* band interleaved data */ |
| 339 |
for (band = 0; band < nbands; band++) |
| 340 |
{ |
| 341 |
if (_TIFF_lseek_f( |
| 342 |
fd, hdr_size + (length * band + row) * linebytes, |
| 343 |
SEEK_SET) == (_TIFF_off_t)-1) |
| 344 |
{ |
| 345 |
fprintf(stderr, |
| 346 |
"%s: %s: scanline %" PRIu32 ": seek error.\n", |
| 347 |
argv[0], argv[optind], row); |
| 348 |
break; |
| 349 |
} |
| 350 |
if (read(fd, buf, linebytes) < 0) |
| 351 |
{ |
| 352 |
fprintf(stderr, |
| 353 |
"%s: %s: scanline %" PRIu32 ": Read error.\n", |
| 354 |
argv[0], argv[optind], row); |
| 355 |
break; |
| 356 |
} |
| 357 |
if (swab) /* Swap bytes if needed */ |
| 358 |
swapBytesInScanline(buf, width, dtype); |
| 359 |
for (col = 0; col < width; col++) |
| 360 |
memcpy(buf1 + (col * nbands + band) * depth, |
| 361 |
buf + col * depth, depth); |
| 362 |
} |
| 363 |
break; |
| 364 |
case PIXEL: /* pixel interleaved data */ |
| 365 |
default: |
| 366 |
if (read(fd, buf1, bufsize) < 0) |
| 367 |
{ |
| 368 |
fprintf(stderr, |
| 369 |
"%s: %s: scanline %" PRIu32 ": Read error.\n", |
| 370 |
argv[0], argv[optind], row); |
| 371 |
break; |
| 372 |
} |
| 373 |
if (swab) /* Swap bytes if needed */ |
| 374 |
swapBytesInScanline(buf1, width, dtype); |
| 375 |
break; |
| 376 |
} |
| 377 |
|
| 378 |
if (TIFFWriteScanline(out, buf1, row, 0) < 0) |
| 379 |
{ |
| 380 |
fprintf(stderr, "%s: %s: scanline %" PRIu32 ": Write error.\n", |
| 381 |
argv[0], outfilename, row); |
| 382 |
break; |
| 383 |
} |
| 384 |
} |
| 385 |
if (buf) |
| 386 |
_TIFFfree(buf); |
| 387 |
if (buf1) |
| 388 |
_TIFFfree(buf1); |
| 389 |
TIFFClose(out); |
| 390 |
return (EXIT_SUCCESS); |
| 391 |
} |
| 392 |
|
| 393 |
static void swapBytesInScanline(void *buf, uint32_t width, TIFFDataType dtype) |
| 394 |
{ |
| 395 |
switch (dtype) |
| 396 |
{ |
| 397 |
case TIFF_SHORT: |
| 398 |
case TIFF_SSHORT: |
| 399 |
TIFFSwabArrayOfShort((uint16_t *)buf, (unsigned long)width); |
| 400 |
break; |
| 401 |
case TIFF_LONG: |
| 402 |
case TIFF_SLONG: |
| 403 |
TIFFSwabArrayOfLong((uint32_t *)buf, (unsigned long)width); |
| 404 |
break; |
| 405 |
/* case TIFF_FLOAT: */ /* FIXME */ |
| 406 |
case TIFF_DOUBLE: |
| 407 |
TIFFSwabArrayOfDouble((double *)buf, (unsigned long)width); |
| 408 |
break; |
| 409 |
default: |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
static int guessSize(int fd, TIFFDataType dtype, _TIFF_off_t hdr_size, |
| 415 |
uint32_t nbands, int swab, uint32_t *width, |
| 416 |
uint32_t *length) |
| 417 |
{ |
| 418 |
const float longt = 40.0; /* maximum possible height/width ratio */ |
| 419 |
char *buf1, *buf2; |
| 420 |
_TIFF_stat_s filestat; |
| 421 |
uint32_t w, h, scanlinesize, imagesize; |
| 422 |
uint32_t depth = TIFFDataWidth(dtype); |
| 423 |
double cor_coef = 0, tmp; |
| 424 |
|
| 425 |
if (_TIFF_fstat_f(fd, &filestat) == -1) |
| 426 |
{ |
| 427 |
fprintf(stderr, "Failed to obtain file size.\n"); |
| 428 |
return -1; |
| 429 |
} |
| 430 |
|
| 431 |
if (filestat.st_size < hdr_size) |
| 432 |
{ |
| 433 |
fprintf(stderr, "Too large header size specified.\n"); |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
imagesize = (filestat.st_size - hdr_size) / nbands / depth; |
| 438 |
|
| 439 |
if (*width != 0 && *length == 0) |
| 440 |
{ |
| 441 |
fprintf(stderr, "Image height is not specified.\n"); |
| 442 |
|
| 443 |
*length = imagesize / *width; |
| 444 |
|
| 445 |
fprintf(stderr, "Height is guessed as %" PRIu32 ".\n", *length); |
| 446 |
|
| 447 |
return 1; |
| 448 |
} |
| 449 |
else if (*width == 0 && *length != 0) |
| 450 |
{ |
| 451 |
fprintf(stderr, "Image width is not specified.\n"); |
| 452 |
|
| 453 |
*width = imagesize / *length; |
| 454 |
|
| 455 |
fprintf(stderr, "Width is guessed as %" PRIu32 ".\n", *width); |
| 456 |
|
| 457 |
return 1; |
| 458 |
} |
| 459 |
else if (*width == 0 && *length == 0) |
| 460 |
{ |
| 461 |
unsigned int fail = 0; |
| 462 |
fprintf(stderr, "Image width and height are not specified.\n"); |
| 463 |
w = (uint32_t)sqrt(imagesize / longt); |
| 464 |
if (w == 0) |
| 465 |
{ |
| 466 |
fprintf(stderr, "Too small image size.\n"); |
| 467 |
return -1; |
| 468 |
} |
| 469 |
|
| 470 |
for (; w < sqrt(imagesize * longt); w++) |
| 471 |
{ |
| 472 |
if (imagesize % w == 0) |
| 473 |
{ |
| 474 |
scanlinesize = w * depth; |
| 475 |
h = imagesize / w; |
| 476 |
if (h < 2) |
| 477 |
continue; |
| 478 |
/* reads 2 lines at the middle of the image and calculate their |
| 479 |
* correlation. it works for h >= 2. (in this case it will |
| 480 |
* compare line 0 and line 1 */ |
| 481 |
buf1 = _TIFFmalloc(scanlinesize); |
| 482 |
buf2 = _TIFFmalloc(scanlinesize); |
| 483 |
do |
| 484 |
{ |
| 485 |
if (_TIFF_lseek_f( |
| 486 |
fd, hdr_size + (int)((h - 1) / 2) * scanlinesize, |
| 487 |
SEEK_SET) == (_TIFF_off_t)-1) |
| 488 |
{ |
| 489 |
fprintf(stderr, "seek error.\n"); |
| 490 |
fail = 1; |
| 491 |
break; |
| 492 |
} |
| 493 |
/* read line (h-1)/2 */ |
| 494 |
if (read(fd, buf1, scanlinesize) != (long)scanlinesize) |
| 495 |
{ |
| 496 |
fprintf(stderr, "read error.\n"); |
| 497 |
fail = 1; |
| 498 |
break; |
| 499 |
} |
| 500 |
/* read line ((h-1)/2)+1 */ |
| 501 |
if (read(fd, buf2, scanlinesize) != (long)scanlinesize) |
| 502 |
{ |
| 503 |
fprintf(stderr, "read error.\n"); |
| 504 |
fail = 1; |
| 505 |
break; |
| 506 |
} |
| 507 |
if (swab) |
| 508 |
{ |
| 509 |
swapBytesInScanline(buf1, w, dtype); |
| 510 |
swapBytesInScanline(buf2, w, dtype); |
| 511 |
} |
| 512 |
if (0 == memcmp(buf1, buf2, scanlinesize)) |
| 513 |
{ |
| 514 |
*width = w, *length = h; |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
tmp = fabs(correlation(buf1, buf2, w, dtype)); |
| 519 |
if (tmp > cor_coef) |
| 520 |
{ |
| 521 |
cor_coef = tmp; |
| 522 |
*width = w, *length = h; |
| 523 |
} |
| 524 |
} |
| 525 |
} while (0); |
| 526 |
|
| 527 |
_TIFFfree(buf1); |
| 528 |
_TIFFfree(buf2); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
if (fail) |
| 533 |
{ |
| 534 |
return -1; |
| 535 |
} |
| 536 |
|
| 537 |
fprintf(stderr, |
| 538 |
"Width is guessed as %" PRIu32 ", height is guessed as %" PRIu32 |
| 539 |
".\n", |
| 540 |
*width, *length); |
| 541 |
|
| 542 |
return 1; |
| 543 |
} |
| 544 |
else |
| 545 |
{ |
| 546 |
if (filestat.st_size < |
| 547 |
(_TIFF_off_t)(hdr_size + (*width) * (*length) * nbands * depth)) |
| 548 |
{ |
| 549 |
fprintf(stderr, "Input file too small.\n"); |
| 550 |
return -1; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
return 1; |
| 555 |
} |
| 556 |
|
| 557 |
/* Calculate correlation coefficient between two numeric vectors */ |
| 558 |
static double correlation(void *buf1, void *buf2, uint32_t n_elem, |
| 559 |
TIFFDataType dtype) |
| 560 |
{ |
| 561 |
double X, Y, M1 = 0.0, M2 = 0.0, D1 = 0.0, D2 = 0.0, K = 0.0; |
| 562 |
uint32_t i; |
| 563 |
|
| 564 |
switch (dtype) |
| 565 |
{ |
| 566 |
case TIFF_BYTE: |
| 567 |
default: |
| 568 |
for (i = 0; i < n_elem; i++) |
| 569 |
{ |
| 570 |
X = ((unsigned char *)buf1)[i]; |
| 571 |
Y = ((unsigned char *)buf2)[i]; |
| 572 |
M1 += X, M2 += Y; |
| 573 |
D1 += X * X, D2 += Y * Y; |
| 574 |
K += X * Y; |
| 575 |
} |
| 576 |
break; |
| 577 |
case TIFF_SBYTE: |
| 578 |
for (i = 0; i < n_elem; i++) |
| 579 |
{ |
| 580 |
X = ((signed char *)buf1)[i]; |
| 581 |
Y = ((signed char *)buf2)[i]; |
| 582 |
M1 += X, M2 += Y; |
| 583 |
D1 += X * X, D2 += Y * Y; |
| 584 |
K += X * Y; |
| 585 |
} |
| 586 |
break; |
| 587 |
case TIFF_SHORT: |
| 588 |
for (i = 0; i < n_elem; i++) |
| 589 |
{ |
| 590 |
X = ((uint16_t *)buf1)[i]; |
| 591 |
Y = ((uint16_t *)buf2)[i]; |
| 592 |
M1 += X, M2 += Y; |
| 593 |
D1 += X * X, D2 += Y * Y; |
| 594 |
K += X * Y; |
| 595 |
} |
| 596 |
break; |
| 597 |
case TIFF_SSHORT: |
| 598 |
for (i = 0; i < n_elem; i++) |
| 599 |
{ |
| 600 |
X = ((int16_t *)buf1)[i]; |
| 601 |
Y = ((int16_t *)buf2)[i]; |
| 602 |
M1 += X, M2 += Y; |
| 603 |
D1 += X * X, D2 += Y * Y; |
| 604 |
K += X * Y; |
| 605 |
} |
| 606 |
break; |
| 607 |
case TIFF_LONG: |
| 608 |
for (i = 0; i < n_elem; i++) |
| 609 |
{ |
| 610 |
X = ((uint32_t *)buf1)[i]; |
| 611 |
Y = ((uint32_t *)buf2)[i]; |
| 612 |
M1 += X, M2 += Y; |
| 613 |
D1 += X * X, D2 += Y * Y; |
| 614 |
K += X * Y; |
| 615 |
} |
| 616 |
break; |
| 617 |
case TIFF_SLONG: |
| 618 |
for (i = 0; i < n_elem; i++) |
| 619 |
{ |
| 620 |
X = ((int32_t *)buf1)[i]; |
| 621 |
Y = ((int32_t *)buf2)[i]; |
| 622 |
M1 += X, M2 += Y; |
| 623 |
D1 += X * X, D2 += Y * Y; |
| 624 |
K += X * Y; |
| 625 |
} |
| 626 |
break; |
| 627 |
case TIFF_FLOAT: |
| 628 |
for (i = 0; i < n_elem; i++) |
| 629 |
{ |
| 630 |
X = ((float *)buf1)[i]; |
| 631 |
Y = ((float *)buf2)[i]; |
| 632 |
M1 += X, M2 += Y; |
| 633 |
D1 += X * X, D2 += Y * Y; |
| 634 |
K += X * Y; |
| 635 |
} |
| 636 |
break; |
| 637 |
case TIFF_DOUBLE: |
| 638 |
for (i = 0; i < n_elem; i++) |
| 639 |
{ |
| 640 |
X = ((double *)buf1)[i]; |
| 641 |
Y = ((double *)buf2)[i]; |
| 642 |
M1 += X, M2 += Y; |
| 643 |
D1 += X * X, D2 += Y * Y; |
| 644 |
K += X * Y; |
| 645 |
} |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
M1 /= n_elem; |
| 650 |
M2 /= n_elem; |
| 651 |
D1 -= M1 * M1 * n_elem; |
| 652 |
D2 -= M2 * M2 * n_elem; |
| 653 |
if (D1 * D2 == 0.0) |
| 654 |
return 0.0; /* avoid divide by zero */ |
| 655 |
K = (K - M1 * M2 * n_elem) / sqrt(D1 * D2); |
| 656 |
|
| 657 |
return K; |
| 658 |
} |
| 659 |
|
| 660 |
static int processCompressOptions(char *opt) |
| 661 |
{ |
| 662 |
if (strcmp(opt, "none") == 0) |
| 663 |
compression = COMPRESSION_NONE; |
| 664 |
else if (strcmp(opt, "packbits") == 0) |
| 665 |
compression = COMPRESSION_PACKBITS; |
| 666 |
else if (strncmp(opt, "jpeg", 4) == 0) |
| 667 |
{ |
| 668 |
char *cp = strchr(opt, ':'); |
| 669 |
|
| 670 |
compression = COMPRESSION_JPEG; |
| 671 |
while (cp) |
| 672 |
{ |
| 673 |
if (isdigit((int)cp[1])) |
| 674 |
quality = atoi(cp + 1); |
| 675 |
else if (cp[1] == 'r') |
| 676 |
jpegcolormode = JPEGCOLORMODE_RAW; |
| 677 |
else |
| 678 |
usage(EXIT_FAILURE); |
| 679 |
|
| 680 |
cp = strchr(cp + 1, ':'); |
| 681 |
} |
| 682 |
} |
| 683 |
else if (strncmp(opt, "lzw", 3) == 0) |
| 684 |
{ |
| 685 |
char *cp = strchr(opt, ':'); |
| 686 |
if (cp) |
| 687 |
predictor = atoi(cp + 1); |
| 688 |
compression = COMPRESSION_LZW; |
| 689 |
} |
| 690 |
else if (strncmp(opt, "zip", 3) == 0) |
| 691 |
{ |
| 692 |
char *cp = strchr(opt, ':'); |
| 693 |
if (cp) |
| 694 |
predictor = atoi(cp + 1); |
| 695 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 696 |
} |
| 697 |
else |
| 698 |
return (0); |
| 699 |
return (1); |
| 700 |
} |
| 701 |
|
| 702 |
static const char usage_info[] = |
| 703 |
"Create a TIFF file from raw data\n\n" |
| 704 |
"usage: raw2tiff [options] input.raw output.tif\n" |
| 705 |
"where options are:\n" |
| 706 |
" -L input data has LSB2MSB bit order (default)\n" |
| 707 |
" -M input data has MSB2LSB bit order\n" |
| 708 |
" -r # make each strip have no more than # rows\n" |
| 709 |
" -H # size of input image file header in bytes (0 by " |
| 710 |
"default)\n" |
| 711 |
" -w # width of input image in pixels\n" |
| 712 |
" -l # length of input image in lines\n" |
| 713 |
" -b # number of bands in input image (1 by default)\n" |
| 714 |
"\n" |
| 715 |
" -d data_type type of samples in input image\n" |
| 716 |
"where data_type may be:\n" |
| 717 |
" byte 8-bit unsigned integer (default)\n" |
| 718 |
" short 16-bit unsigned integer\n" |
| 719 |
" long 32-bit unsigned integer\n" |
| 720 |
" sbyte 8-bit signed integer\n" |
| 721 |
" sshort 16-bit signed integer\n" |
| 722 |
" slong 32-bit signed integer\n" |
| 723 |
" float 32-bit IEEE floating point\n" |
| 724 |
" double 64-bit IEEE floating point\n" |
| 725 |
"\n" |
| 726 |
" -p photo photometric interpretation (color space) of the input image\n" |
| 727 |
"where photo may be:\n" |
| 728 |
" miniswhite white color represented with 0 value\n" |
| 729 |
" minisblack black color represented with 0 value (default)\n" |
| 730 |
" rgb image has RGB color model\n" |
| 731 |
" cmyk image has CMYK (separated) color model\n" |
| 732 |
" ycbcr image has YCbCr color model\n" |
| 733 |
" cielab image has CIE L*a*b color model\n" |
| 734 |
" icclab image has ICC L*a*b color model\n" |
| 735 |
" itulab image has ITU L*a*b color model\n" |
| 736 |
"\n" |
| 737 |
" -s swap bytes fetched from input file\n" |
| 738 |
"\n" |
| 739 |
" -i config type of samples interleaving in input image\n" |
| 740 |
"where config may be:\n" |
| 741 |
" pixel pixel interleaved data (default)\n" |
| 742 |
" band band interleaved data\n" |
| 743 |
"\n" |
| 744 |
#ifdef LZW_SUPPORT |
| 745 |
" -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding\n" |
| 746 |
/* " LZW options:\n" */ |
| 747 |
" # set predictor value\n" |
| 748 |
" For example, -c lzw:2 for LZW-encoded data with horizontal " |
| 749 |
"differencing\n" |
| 750 |
#endif |
| 751 |
#ifdef ZIP_SUPPORT |
| 752 |
" -c zip[:opts] compress output with deflate encoding\n" |
| 753 |
/* " Deflate (ZIP) options:\n" */ |
| 754 |
" # set predictor value\n" |
| 755 |
#endif |
| 756 |
#ifdef JPEG_SUPPORT |
| 757 |
" -c jpeg[:opts] compress output with JPEG encoding\n" |
| 758 |
/* " JPEG options:\n" */ |
| 759 |
" # set compression quality level (0-100, default 75)\n" |
| 760 |
" r output color image as RGB rather than YCbCr\n" |
| 761 |
" For example, -c jpeg:r:50 for JPEG-encoded RGB data with 50% comp. " |
| 762 |
"quality\n" |
| 763 |
#endif |
| 764 |
#ifdef PACKBITS_SUPPORT |
| 765 |
" -c packbits compress output with packbits encoding\n" |
| 766 |
#endif |
| 767 |
#if defined(LZW_SUPPORT) || defined(ZIP_SUPPORT) || defined(JPEG_SUPPORT) || \ |
| 768 |
defined(PACKBITS_SUPPORT) |
| 769 |
" -c none use no compression algorithm on output\n" |
| 770 |
#endif |
| 771 |
"\n" |
| 772 |
" -o out.tif write output to out.tif\n" |
| 773 |
" -h this help message\n"; |
| 774 |
|
| 775 |
static void usage(int code) |
| 776 |
{ |
| 777 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 778 |
|
| 779 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 780 |
fprintf(out, "%s", usage_info); |
| 781 |
exit(code); |
| 782 |
} |