| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* Copyright (c) 2003, Andrey Kiselev <[email protected]> |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 7 |
* its documentation for any purpose is hereby granted without fee, provided |
| 8 |
* that (i) the above copyright notices and this permission notice appear in |
| 9 |
* all copies of the software and related documentation, and (ii) the names of |
| 10 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 11 |
* publicity relating to the software without the specific, prior written |
| 12 |
* permission of Sam Leffler and Silicon Graphics. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 16 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
* |
| 18 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 19 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 20 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 21 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 22 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 |
* OF THIS SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "libport.h" |
| 27 |
#include "tif_config.h" |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#if HAVE_UNISTD_H |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifdef HAVE_OPENGL_GL_H |
| 37 |
#include <OpenGL/gl.h> |
| 38 |
#else |
| 39 |
#ifdef _WIN32 |
| 40 |
#include <windows.h> |
| 41 |
#endif |
| 42 |
#include <GL/gl.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_GLUT_GLUT_H |
| 45 |
#include <GLUT/glut.h> |
| 46 |
#else |
| 47 |
#include <GL/glut.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "tiffio.h" |
| 51 |
#include "tiffiop.h" |
| 52 |
|
| 53 |
#ifndef EXIT_SUCCESS |
| 54 |
#define EXIT_SUCCESS 0 |
| 55 |
#endif |
| 56 |
#ifndef EXIT_FAILURE |
| 57 |
#define EXIT_FAILURE 1 |
| 58 |
#endif |
| 59 |
|
| 60 |
static uint32_t width = 0, height = 0; /* window width & height */ |
| 61 |
static uint32_t *raster = NULL; /* displayable image */ |
| 62 |
static TIFFRGBAImage img; |
| 63 |
static int order0 = 0, order; |
| 64 |
static uint16_t photo0 = (uint16_t)-1, photo; |
| 65 |
static int stoponerr = 0; /* stop on read error */ |
| 66 |
static int verbose = 0; |
| 67 |
#define TITLE_LENGTH 1024 |
| 68 |
static char title[TITLE_LENGTH]; /* window title line */ |
| 69 |
static uint32_t xmax, ymax; |
| 70 |
static char **filelist = NULL; |
| 71 |
static int fileindex; |
| 72 |
static int filenum; |
| 73 |
static TIFFErrorHandler oerror; |
| 74 |
static TIFFErrorHandler owarning; |
| 75 |
|
| 76 |
static void cleanup_and_exit(int); |
| 77 |
static int initImage(void); |
| 78 |
static int prevImage(void); |
| 79 |
static int nextImage(void); |
| 80 |
static void setWindowSize(void); |
| 81 |
static void usage(int); |
| 82 |
static uint16_t photoArg(const char *); |
| 83 |
static void raster_draw(void); |
| 84 |
static void raster_reshape(int, int); |
| 85 |
static void raster_keys(unsigned char, int, int); |
| 86 |
static void raster_special(int, int, int); |
| 87 |
|
| 88 |
#if !HAVE_DECL_OPTARG |
| 89 |
extern char *optarg; |
| 90 |
extern int optind; |
| 91 |
#endif |
| 92 |
|
| 93 |
/* GLUT framework on MacOS X produces deprecation warnings */ |
| 94 |
#if defined(__GNUC__) && defined(__APPLE__) |
| 95 |
#pragma GCC diagnostic push |
| 96 |
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 97 |
#endif |
| 98 |
|
| 99 |
static TIFF *tif = NULL; |
| 100 |
|
| 101 |
int main(int argc, char *argv[]) |
| 102 |
{ |
| 103 |
int c; |
| 104 |
int dirnum = -1; |
| 105 |
uint32_t diroff = 0; |
| 106 |
|
| 107 |
oerror = TIFFSetErrorHandler(NULL); |
| 108 |
owarning = TIFFSetWarningHandler(NULL); |
| 109 |
while ((c = getopt(argc, argv, "d:o:p:eflmsvwh")) != -1) |
| 110 |
switch (c) |
| 111 |
{ |
| 112 |
case 'd': |
| 113 |
dirnum = atoi(optarg); |
| 114 |
break; |
| 115 |
case 'e': |
| 116 |
oerror = TIFFSetErrorHandler(oerror); |
| 117 |
break; |
| 118 |
case 'l': |
| 119 |
order0 = FILLORDER_LSB2MSB; |
| 120 |
break; |
| 121 |
case 'm': |
| 122 |
order0 = FILLORDER_MSB2LSB; |
| 123 |
break; |
| 124 |
case 'o': |
| 125 |
diroff = strtoul(optarg, NULL, 0); |
| 126 |
break; |
| 127 |
case 'p': |
| 128 |
photo0 = photoArg(optarg); |
| 129 |
break; |
| 130 |
case 's': |
| 131 |
stoponerr = 1; |
| 132 |
break; |
| 133 |
case 'w': |
| 134 |
owarning = TIFFSetWarningHandler(owarning); |
| 135 |
break; |
| 136 |
case 'v': |
| 137 |
verbose = 1; |
| 138 |
break; |
| 139 |
case 'h': |
| 140 |
usage(EXIT_SUCCESS); |
| 141 |
/*NOTREACHED*/ |
| 142 |
break; |
| 143 |
case '?': |
| 144 |
usage(EXIT_FAILURE); |
| 145 |
/*NOTREACHED*/ |
| 146 |
break; |
| 147 |
} |
| 148 |
filenum = argc - optind; |
| 149 |
if (filenum < 1) |
| 150 |
usage(EXIT_FAILURE); |
| 151 |
|
| 152 |
glutInit(&argc, argv); |
| 153 |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); |
| 154 |
|
| 155 |
/* |
| 156 |
* Get the screen size |
| 157 |
*/ |
| 158 |
xmax = glutGet(GLUT_SCREEN_WIDTH); |
| 159 |
ymax = glutGet(GLUT_SCREEN_HEIGHT); |
| 160 |
|
| 161 |
/* |
| 162 |
* Use 90% of the screen size |
| 163 |
*/ |
| 164 |
xmax = xmax - xmax / 10.0; |
| 165 |
ymax = ymax - ymax / 10.0; |
| 166 |
|
| 167 |
filelist = (char **)_TIFFmalloc(filenum * sizeof(char *)); |
| 168 |
if (!filelist) |
| 169 |
{ |
| 170 |
TIFFError(argv[0], "Can not allocate space for the file list."); |
| 171 |
return EXIT_FAILURE; |
| 172 |
} |
| 173 |
_TIFFmemcpy(filelist, argv + optind, filenum * sizeof(char *)); |
| 174 |
fileindex = -1; |
| 175 |
if (nextImage() < 0) |
| 176 |
{ |
| 177 |
_TIFFfree(filelist); |
| 178 |
return EXIT_FAILURE; |
| 179 |
} |
| 180 |
/* |
| 181 |
* Set initial directory if user-specified |
| 182 |
* file was opened successfully. |
| 183 |
*/ |
| 184 |
if (dirnum != -1 && !TIFFSetDirectory(tif, dirnum)) |
| 185 |
TIFFError(argv[0], "Error, seeking to directory %d", dirnum); |
| 186 |
if (diroff != 0 && !TIFFSetSubDirectory(tif, diroff)) |
| 187 |
TIFFError(argv[0], "Error, setting subdirectory at %#x", diroff); |
| 188 |
order = order0; |
| 189 |
photo = photo0; |
| 190 |
if (initImage() < 0) |
| 191 |
{ |
| 192 |
_TIFFfree(filelist); |
| 193 |
return EXIT_FAILURE; |
| 194 |
} |
| 195 |
/* |
| 196 |
* Create a new window or reconfigure an existing |
| 197 |
* one to suit the image to be displayed. |
| 198 |
*/ |
| 199 |
glutInitWindowSize(width, height); |
| 200 |
snprintf(title, TITLE_LENGTH - 1, "%s [%u]", filelist[fileindex], |
| 201 |
TIFFCurrentDirectory(tif)); |
| 202 |
glutCreateWindow(title); |
| 203 |
glutDisplayFunc(raster_draw); |
| 204 |
glutReshapeFunc(raster_reshape); |
| 205 |
glutKeyboardFunc(raster_keys); |
| 206 |
glutSpecialFunc(raster_special); |
| 207 |
glutMainLoop(); |
| 208 |
|
| 209 |
cleanup_and_exit(EXIT_SUCCESS); |
| 210 |
return EXIT_SUCCESS; |
| 211 |
} |
| 212 |
|
| 213 |
static void cleanup_and_exit(int code) |
| 214 |
{ |
| 215 |
TIFFRGBAImageEnd(&img); |
| 216 |
if (filelist != NULL) |
| 217 |
_TIFFfree(filelist); |
| 218 |
if (raster != NULL) |
| 219 |
_TIFFfree(raster); |
| 220 |
if (tif != NULL) |
| 221 |
TIFFClose(tif); |
| 222 |
exit(code); |
| 223 |
} |
| 224 |
|
| 225 |
static int initImage(void) |
| 226 |
{ |
| 227 |
uint32_t w, h; |
| 228 |
|
| 229 |
if (order) |
| 230 |
TIFFSetField(tif, TIFFTAG_FILLORDER, order); |
| 231 |
if (photo != (uint16_t)-1) |
| 232 |
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photo); |
| 233 |
if (!TIFFRGBAImageBegin(&img, tif, stoponerr, title)) |
| 234 |
{ |
| 235 |
TIFFError(filelist[fileindex], "%s", title); |
| 236 |
TIFFClose(tif); |
| 237 |
tif = NULL; |
| 238 |
return -1; |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* Setup the image raster as required. |
| 243 |
*/ |
| 244 |
h = img.height; |
| 245 |
w = img.width; |
| 246 |
if (h > ymax) |
| 247 |
{ |
| 248 |
w = (int)(w * ((float)ymax / h)); |
| 249 |
h = ymax; |
| 250 |
} |
| 251 |
if (w > xmax) |
| 252 |
{ |
| 253 |
h = (int)(h * ((float)xmax / w)); |
| 254 |
w = xmax; |
| 255 |
} |
| 256 |
|
| 257 |
if (w != width || h != height) |
| 258 |
{ |
| 259 |
uint32_t rastersize = _TIFFMultiply32(tif, img.width, img.height, |
| 260 |
"allocating raster buffer"); |
| 261 |
if (raster != NULL) |
| 262 |
_TIFFfree(raster), raster = NULL; |
| 263 |
raster = (uint32_t *)_TIFFCheckMalloc(tif, rastersize, sizeof(uint32_t), |
| 264 |
"allocating raster buffer"); |
| 265 |
if (raster == NULL) |
| 266 |
{ |
| 267 |
width = height = 0; |
| 268 |
TIFFError(filelist[fileindex], "No space for raster buffer"); |
| 269 |
cleanup_and_exit(EXIT_FAILURE); |
| 270 |
} |
| 271 |
width = w; |
| 272 |
height = h; |
| 273 |
} |
| 274 |
TIFFRGBAImageGet(&img, raster, img.width, img.height); |
| 275 |
#if HOST_BIGENDIAN |
| 276 |
TIFFSwabArrayOfLong(raster, img.width * img.height); |
| 277 |
#endif |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
static int prevImage(void) |
| 282 |
{ |
| 283 |
if (fileindex > 0) |
| 284 |
fileindex--; |
| 285 |
else if (tif) |
| 286 |
return fileindex; |
| 287 |
if (tif) |
| 288 |
TIFFClose(tif); |
| 289 |
tif = TIFFOpen(filelist[fileindex], "r"); |
| 290 |
if (tif == NULL) |
| 291 |
return -1; |
| 292 |
return fileindex; |
| 293 |
} |
| 294 |
|
| 295 |
static int nextImage(void) |
| 296 |
{ |
| 297 |
if (fileindex < filenum - 1) |
| 298 |
fileindex++; |
| 299 |
else if (tif) |
| 300 |
return fileindex; |
| 301 |
if (tif) |
| 302 |
TIFFClose(tif); |
| 303 |
tif = TIFFOpen(filelist[fileindex], "r"); |
| 304 |
if (tif == NULL) |
| 305 |
return -1; |
| 306 |
return fileindex; |
| 307 |
} |
| 308 |
|
| 309 |
static void setWindowSize(void) { glutReshapeWindow(width, height); } |
| 310 |
|
| 311 |
static void raster_draw(void) |
| 312 |
{ |
| 313 |
glDrawPixels(img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, |
| 314 |
(const GLvoid *)raster); |
| 315 |
glFlush(); |
| 316 |
} |
| 317 |
|
| 318 |
static void raster_reshape(int win_w, int win_h) |
| 319 |
{ |
| 320 |
GLfloat xratio = (GLfloat)win_w / img.width; |
| 321 |
GLfloat yratio = (GLfloat)win_h / img.height; |
| 322 |
int ratio = (int)(((xratio > yratio) ? xratio : yratio) * 100); |
| 323 |
|
| 324 |
glPixelZoom(xratio, yratio); |
| 325 |
glViewport(0, 0, win_w, win_h); |
| 326 |
snprintf(title, 1024, "%s [%u] %d%%", filelist[fileindex], |
| 327 |
TIFFCurrentDirectory(tif), ratio); |
| 328 |
glutSetWindowTitle(title); |
| 329 |
} |
| 330 |
|
| 331 |
static void raster_keys(unsigned char key, int x, int y) |
| 332 |
{ |
| 333 |
(void)x; |
| 334 |
(void)y; |
| 335 |
switch (key) |
| 336 |
{ |
| 337 |
case 'b': /* photometric MinIsBlack */ |
| 338 |
photo = PHOTOMETRIC_MINISBLACK; |
| 339 |
initImage(); |
| 340 |
break; |
| 341 |
case 'l': /* lsb-to-msb FillOrder */ |
| 342 |
order = FILLORDER_LSB2MSB; |
| 343 |
initImage(); |
| 344 |
break; |
| 345 |
case 'm': /* msb-to-lsb FillOrder */ |
| 346 |
order = FILLORDER_MSB2LSB; |
| 347 |
initImage(); |
| 348 |
break; |
| 349 |
case 'w': /* photometric MinIsWhite */ |
| 350 |
photo = PHOTOMETRIC_MINISWHITE; |
| 351 |
initImage(); |
| 352 |
break; |
| 353 |
case 'W': /* toggle warnings */ |
| 354 |
owarning = TIFFSetWarningHandler(owarning); |
| 355 |
initImage(); |
| 356 |
break; |
| 357 |
case 'E': /* toggle errors */ |
| 358 |
oerror = TIFFSetErrorHandler(oerror); |
| 359 |
initImage(); |
| 360 |
break; |
| 361 |
case 'z': /* reset to defaults */ |
| 362 |
case 'Z': |
| 363 |
order = order0; |
| 364 |
photo = photo0; |
| 365 |
if (owarning == NULL) |
| 366 |
owarning = TIFFSetWarningHandler(NULL); |
| 367 |
if (oerror == NULL) |
| 368 |
oerror = TIFFSetErrorHandler(NULL); |
| 369 |
initImage(); |
| 370 |
break; |
| 371 |
case 'q': /* exit */ |
| 372 |
case '\033': |
| 373 |
cleanup_and_exit(EXIT_SUCCESS); |
| 374 |
} |
| 375 |
glutPostRedisplay(); |
| 376 |
} |
| 377 |
|
| 378 |
static void raster_special(int key, int x, int y) |
| 379 |
{ |
| 380 |
(void)x; |
| 381 |
(void)y; |
| 382 |
switch (key) |
| 383 |
{ |
| 384 |
case GLUT_KEY_PAGE_UP: /* previous logical image */ |
| 385 |
if (TIFFCurrentDirectory(tif) > 0) |
| 386 |
{ |
| 387 |
if (TIFFSetDirectory(tif, TIFFCurrentDirectory(tif) - 1)) |
| 388 |
{ |
| 389 |
initImage(); |
| 390 |
setWindowSize(); |
| 391 |
} |
| 392 |
} |
| 393 |
else |
| 394 |
{ |
| 395 |
TIFFRGBAImageEnd(&img); |
| 396 |
prevImage(); |
| 397 |
initImage(); |
| 398 |
setWindowSize(); |
| 399 |
} |
| 400 |
break; |
| 401 |
case GLUT_KEY_PAGE_DOWN: /* next logical image */ |
| 402 |
if (!TIFFLastDirectory(tif)) |
| 403 |
{ |
| 404 |
if (TIFFReadDirectory(tif)) |
| 405 |
{ |
| 406 |
initImage(); |
| 407 |
setWindowSize(); |
| 408 |
} |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
TIFFRGBAImageEnd(&img); |
| 413 |
nextImage(); |
| 414 |
initImage(); |
| 415 |
setWindowSize(); |
| 416 |
} |
| 417 |
break; |
| 418 |
case GLUT_KEY_HOME: /* 1st image in current file */ |
| 419 |
if (TIFFSetDirectory(tif, 0)) |
| 420 |
{ |
| 421 |
TIFFRGBAImageEnd(&img); |
| 422 |
initImage(); |
| 423 |
setWindowSize(); |
| 424 |
} |
| 425 |
break; |
| 426 |
case GLUT_KEY_END: /* last image in current file */ |
| 427 |
TIFFRGBAImageEnd(&img); |
| 428 |
while (!TIFFLastDirectory(tif)) |
| 429 |
TIFFReadDirectory(tif); |
| 430 |
initImage(); |
| 431 |
setWindowSize(); |
| 432 |
break; |
| 433 |
} |
| 434 |
glutPostRedisplay(); |
| 435 |
} |
| 436 |
|
| 437 |
/* GLUT framework on MacOS X produces deprecation warnings */ |
| 438 |
#if defined(__GNUC__) && defined(__APPLE__) |
| 439 |
#pragma GCC diagnostic pop |
| 440 |
#endif |
| 441 |
|
| 442 |
static const char *stuff[] = { |
| 443 |
"usage: tiffgt [options] file.tif", |
| 444 |
"where options are:", |
| 445 |
" -c use colormap visual", |
| 446 |
" -d dirnum set initial directory (default is 0)", |
| 447 |
" -e enable display of TIFF error messages", |
| 448 |
" -l force lsb-to-msb FillOrder", |
| 449 |
" -m force msb-to-lsb FillOrder", |
| 450 |
" -o offset set initial directory offset", |
| 451 |
" -p photo override photometric interpretation", |
| 452 |
" -r use fullcolor visual", |
| 453 |
" -s stop decoding on first error (default is ignore errors)", |
| 454 |
" -v enable verbose mode", |
| 455 |
" -w enable display of TIFF warning messages", |
| 456 |
NULL}; |
| 457 |
|
| 458 |
static void usage(int code) |
| 459 |
{ |
| 460 |
int i; |
| 461 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 462 |
|
| 463 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 464 |
for (i = 0; stuff[i] != NULL; i++) |
| 465 |
fprintf(out, "%s\n", stuff[i]); |
| 466 |
exit(code); |
| 467 |
} |
| 468 |
|
| 469 |
static uint16_t photoArg(const char *arg) |
| 470 |
{ |
| 471 |
if (strcmp(arg, "miniswhite") == 0) |
| 472 |
return (PHOTOMETRIC_MINISWHITE); |
| 473 |
else if (strcmp(arg, "minisblack") == 0) |
| 474 |
return (PHOTOMETRIC_MINISBLACK); |
| 475 |
else if (strcmp(arg, "rgb") == 0) |
| 476 |
return (PHOTOMETRIC_RGB); |
| 477 |
else if (strcmp(arg, "palette") == 0) |
| 478 |
return (PHOTOMETRIC_PALETTE); |
| 479 |
else if (strcmp(arg, "mask") == 0) |
| 480 |
return (PHOTOMETRIC_MASK); |
| 481 |
else if (strcmp(arg, "separated") == 0) |
| 482 |
return (PHOTOMETRIC_SEPARATED); |
| 483 |
else if (strcmp(arg, "ycbcr") == 0) |
| 484 |
return (PHOTOMETRIC_YCBCR); |
| 485 |
else if (strcmp(arg, "cielab") == 0) |
| 486 |
return (PHOTOMETRIC_CIELAB); |
| 487 |
else if (strcmp(arg, "logl") == 0) |
| 488 |
return (PHOTOMETRIC_LOGL); |
| 489 |
else if (strcmp(arg, "logluv") == 0) |
| 490 |
return (PHOTOMETRIC_LOGLUV); |
| 491 |
else |
| 492 |
return ((uint16_t)-1); |
| 493 |
} |