| 1 |
/* |
| 2 |
* xtiff - view a TIFF file in an X window |
| 3 |
* |
| 4 |
* Dan Sears |
| 5 |
* Chris Sears |
| 6 |
* |
| 7 |
* Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts. |
| 8 |
* |
| 9 |
* All Rights Reserved |
| 10 |
* |
| 11 |
* Permission to use, copy, modify, and distribute this software and its |
| 12 |
* documentation for any purpose and without fee is hereby granted, |
| 13 |
* provided that the above copyright notice appear in all copies and that |
| 14 |
* both that copyright notice and this permission notice appear in |
| 15 |
* supporting documentation, and that the name of Digital not be |
| 16 |
* used in advertising or publicity pertaining to distribution of the |
| 17 |
* software without specific, written prior permission. |
| 18 |
* |
| 19 |
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING |
| 20 |
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL |
| 21 |
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR |
| 22 |
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 23 |
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 24 |
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 25 |
* SOFTWARE. |
| 26 |
* |
| 27 |
* Revision 1.0 90/05/07 |
| 28 |
* Initial release. |
| 29 |
* Revision 2.0 90/12/20 |
| 30 |
* Converted to use the Athena Widgets and the Xt Intrinsics. |
| 31 |
* |
| 32 |
* Notes: |
| 33 |
* |
| 34 |
* According to the TIFF 5.0 Specification, it is possible to have |
| 35 |
* both a TIFFTAG_COLORMAP and a TIFFTAG_COLORRESPONSECURVE. This |
| 36 |
* doesn't make sense since a TIFFTAG_COLORMAP is 16 bits wide and |
| 37 |
* a TIFFTAG_COLORRESPONSECURVE is tfBitsPerSample bits wide for each |
| 38 |
* channel. This is probably a bug in the specification. |
| 39 |
* In this case, TIFFTAG_COLORRESPONSECURVE is ignored. |
| 40 |
* This might make sense if TIFFTAG_COLORMAP was 8 bits wide. |
| 41 |
* |
| 42 |
* TIFFTAG_COLORMAP is often incorrectly written as ranging from |
| 43 |
* 0 to 255 rather than from 0 to 65535. CheckAndCorrectColormap() |
| 44 |
* takes care of this. |
| 45 |
* |
| 46 |
* Only ORIENTATION_TOPLEFT is supported correctly. This is the |
| 47 |
* default TIFF and X orientation. Other orientations will be |
| 48 |
* displayed incorrectly. |
| 49 |
* |
| 50 |
* There is no support for or use of 3/3/2 DirectColor visuals. |
| 51 |
* TIFFTAG_MINSAMPLEVALUE and TIFFTAG_MAXSAMPLEVALUE are not supported. |
| 52 |
* |
| 53 |
* Only TIFFTAG_BITSPERSAMPLE values that are 1, 2, 4 or 8 are supported. |
| 54 |
*/ |
| 55 |
#include <X11/Intrinsic.h> |
| 56 |
#include <X11/Shell.h> |
| 57 |
#include <X11/StringDefs.h> |
| 58 |
#include <X11/Xatom.h> |
| 59 |
#include <X11/Xaw/Form.h> |
| 60 |
#include <X11/Xaw/Label.h> |
| 61 |
#include <X11/Xaw/List.h> |
| 62 |
#include <X11/Xproto.h> |
| 63 |
#include <X11/cursorfont.h> |
| 64 |
#include <math.h> |
| 65 |
#include <stdio.h> |
| 66 |
#include <stdlib.h> |
| 67 |
#include <tiffio.h> |
| 68 |
#define XK_MISCELLANY |
| 69 |
#include "xtifficon.h" |
| 70 |
#include <X11/keysymdef.h> |
| 71 |
|
| 72 |
#define TIFF_GAMMA "2.2" /* default gamma from the TIFF 5.0 spec */ |
| 73 |
#define ROUND(x) (uint16_t)((x) + 0.5) |
| 74 |
#define SCALE(x, s) (((x)*65535L) / (s)) |
| 75 |
#define MCHECK(m) \ |
| 76 |
if (!m) \ |
| 77 |
{ \ |
| 78 |
fprintf(stderr, "malloc failed\n"); \ |
| 79 |
exit(0); \ |
| 80 |
} |
| 81 |
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 82 |
#define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 83 |
#define VIEWPORT_WIDTH 700 |
| 84 |
#define VIEWPORT_HEIGHT 500 |
| 85 |
#define KEY_TRANSLATE 20 |
| 86 |
|
| 87 |
#ifdef __STDC__ |
| 88 |
#define PP(args) args |
| 89 |
#else |
| 90 |
#define PP(args) () |
| 91 |
#endif |
| 92 |
|
| 93 |
int main PP((int argc, char **argv)); |
| 94 |
void OpenTIFFFile PP((void)); |
| 95 |
void GetTIFFHeader PP((void)); |
| 96 |
void SetNameLabel PP((void)); |
| 97 |
void CheckAndCorrectColormap PP((void)); |
| 98 |
void SimpleGammaCorrection PP((void)); |
| 99 |
void GetVisual PP((void)); |
| 100 |
Boolean SearchVisualList PP((int image_depth, int visual_class, |
| 101 |
Visual **visual)); |
| 102 |
void GetTIFFImage PP((void)); |
| 103 |
void CreateXImage PP((void)); |
| 104 |
XtCallbackProc SelectProc PP((Widget w, caddr_t unused_1, caddr_t unused_2)); |
| 105 |
void QuitProc PP((void)); |
| 106 |
void NextProc PP((void)); |
| 107 |
void PreviousProc PP((void)); |
| 108 |
void PageProc PP((int direction)); |
| 109 |
void EventProc PP((Widget widget, caddr_t unused, XEvent *event)); |
| 110 |
void ResizeProc PP((void)); |
| 111 |
int XTiffErrorHandler PP((Display * display, XErrorEvent *error_event)); |
| 112 |
void Usage PP((void)); |
| 113 |
|
| 114 |
int xtVersion = XtSpecificationRelease; /* xtiff depends on R4 or higher */ |
| 115 |
|
| 116 |
/* |
| 117 |
* Xt data structures |
| 118 |
*/ |
| 119 |
Widget shellWidget, formWidget, listWidget, labelWidget, imageWidget; |
| 120 |
|
| 121 |
enum |
| 122 |
{ |
| 123 |
ButtonQuit = 0, |
| 124 |
ButtonPreviousPage = 1, |
| 125 |
ButtonNextPage = 2 |
| 126 |
}; |
| 127 |
|
| 128 |
String buttonStrings[] = {"Quit", "Previous", "Next"}; |
| 129 |
|
| 130 |
static XrmOptionDescRec shellOptions[] = { |
| 131 |
{"-help", "*help", XrmoptionNoArg, (caddr_t) "True"}, |
| 132 |
{"-gamma", "*gamma", XrmoptionSepArg, NULL}, |
| 133 |
{"-usePixmap", "*usePixmap", XrmoptionSepArg, NULL}, |
| 134 |
{"-viewportWidth", "*viewportWidth", XrmoptionSepArg, NULL}, |
| 135 |
{"-viewportHeight", "*viewportHeight", XrmoptionSepArg, NULL}, |
| 136 |
{"-translate", "*translate", XrmoptionSepArg, NULL}, |
| 137 |
{"-verbose", "*verbose", XrmoptionSepArg, NULL}}; |
| 138 |
|
| 139 |
typedef struct |
| 140 |
{ |
| 141 |
Boolean help; |
| 142 |
float gamma; |
| 143 |
Boolean usePixmap; |
| 144 |
uint32_t viewportWidth; |
| 145 |
uint32_t viewportHeight; |
| 146 |
int translate; |
| 147 |
Boolean verbose; |
| 148 |
} AppData, *AppDataPtr; |
| 149 |
|
| 150 |
AppData appData; |
| 151 |
|
| 152 |
XtResource clientResources[] = { |
| 153 |
{"help", XtCBoolean, XtRBoolean, sizeof(Boolean), |
| 154 |
XtOffset(AppDataPtr, help), XtRImmediate, (XtPointer)False}, |
| 155 |
{"gamma", "Gamma", XtRFloat, sizeof(float), XtOffset(AppDataPtr, gamma), |
| 156 |
XtRString, (XtPointer)TIFF_GAMMA}, |
| 157 |
{"usePixmap", "UsePixmap", XtRBoolean, sizeof(Boolean), |
| 158 |
XtOffset(AppDataPtr, usePixmap), XtRImmediate, (XtPointer)True}, |
| 159 |
{"viewportWidth", "ViewportWidth", XtRInt, sizeof(int), |
| 160 |
XtOffset(AppDataPtr, viewportWidth), XtRImmediate, |
| 161 |
(XtPointer)VIEWPORT_WIDTH}, |
| 162 |
{"viewportHeight", "ViewportHeight", XtRInt, sizeof(int), |
| 163 |
XtOffset(AppDataPtr, viewportHeight), XtRImmediate, |
| 164 |
(XtPointer)VIEWPORT_HEIGHT}, |
| 165 |
{"translate", "Translate", XtRInt, sizeof(int), |
| 166 |
XtOffset(AppDataPtr, translate), XtRImmediate, (XtPointer)KEY_TRANSLATE}, |
| 167 |
{"verbose", "Verbose", XtRBoolean, sizeof(Boolean), |
| 168 |
XtOffset(AppDataPtr, verbose), XtRImmediate, (XtPointer)True}}; |
| 169 |
|
| 170 |
Arg formArgs[] = {{XtNresizable, True}}; |
| 171 |
|
| 172 |
Arg listArgs[] = { |
| 173 |
{XtNresizable, False}, {XtNborderWidth, 0}, |
| 174 |
{XtNdefaultColumns, 3}, {XtNforceColumns, True}, |
| 175 |
{XtNlist, (int)buttonStrings}, {XtNnumberStrings, XtNumber(buttonStrings)}, |
| 176 |
{XtNtop, XtChainTop}, {XtNleft, XtChainLeft}, |
| 177 |
{XtNbottom, XtChainTop}, {XtNright, XtChainLeft}}; |
| 178 |
|
| 179 |
Arg labelArgs[] = {{XtNresizable, False}, {XtNwidth, 200}, |
| 180 |
{XtNborderWidth, 0}, {XtNjustify, XtJustifyLeft}, |
| 181 |
{XtNtop, XtChainTop}, {XtNleft, XtChainLeft}, |
| 182 |
{XtNbottom, XtChainTop}, {XtNright, XtChainLeft}}; |
| 183 |
|
| 184 |
Arg imageArgs[] = {{XtNresizable, True}, {XtNborderWidth, 0}, |
| 185 |
{XtNtop, XtChainTop}, {XtNleft, XtChainLeft}, |
| 186 |
{XtNbottom, XtChainTop}, {XtNright, XtChainLeft}}; |
| 187 |
|
| 188 |
XtActionsRec actionsTable[] = {{"quit", QuitProc}, |
| 189 |
{"next", NextProc}, |
| 190 |
{"previous", PreviousProc}, |
| 191 |
{"notifyresize", ResizeProc}}; |
| 192 |
|
| 193 |
char translationsTable[] = "<Key>q: quit() \n \ |
| 194 |
<Key>Q: quit() \n \ |
| 195 |
<Message>WM_PROTOCOLS: quit()\n \ |
| 196 |
<Key>p: previous() \n \ |
| 197 |
<Key>P: previous() \n \ |
| 198 |
<Key>n: next() \n \ |
| 199 |
<Key>N: next() \n \ |
| 200 |
<Configure>: notifyresize()"; |
| 201 |
|
| 202 |
/* |
| 203 |
* X data structures |
| 204 |
*/ |
| 205 |
Colormap xColormap; |
| 206 |
Display *xDisplay; |
| 207 |
Pixmap xImagePixmap; |
| 208 |
Visual *xVisual; |
| 209 |
XImage *xImage; |
| 210 |
GC xWinGc; |
| 211 |
int xImageDepth, xScreen, xRedMask, xGreenMask, xBlueMask, |
| 212 |
xOffset = 0, yOffset = 0, grabX = -1, grabY = -1; |
| 213 |
unsigned char basePixel = 0; |
| 214 |
|
| 215 |
/* |
| 216 |
* TIFF data structures |
| 217 |
*/ |
| 218 |
TIFF *tfFile = NULL; |
| 219 |
uint32_t tfImageWidth, tfImageHeight; |
| 220 |
uint16_t tfBitsPerSample, tfSamplesPerPixel, tfPlanarConfiguration, |
| 221 |
tfPhotometricInterpretation, tfGrayResponseUnit, tfImageDepth, |
| 222 |
tfBytesPerRow; |
| 223 |
int tfDirectory = 0, tfMultiPage = False; |
| 224 |
double tfUnitMap, |
| 225 |
tfGrayResponseUnitMap[] = {-1, -10, -100, -1000, -10000, -100000}; |
| 226 |
|
| 227 |
/* |
| 228 |
* display data structures |
| 229 |
*/ |
| 230 |
double *dRed, *dGreen, *dBlue; |
| 231 |
|
| 232 |
/* |
| 233 |
* shared data structures |
| 234 |
*/ |
| 235 |
uint16_t *redMap = NULL, *greenMap = NULL, *blueMap = NULL, *grayMap = NULL, |
| 236 |
colormapSize; |
| 237 |
char *imageMemory; |
| 238 |
char *fileName; |
| 239 |
|
| 240 |
int main(int argc, char **argv) |
| 241 |
{ |
| 242 |
XSetWindowAttributes window_attributes; |
| 243 |
Widget widget_list[3]; |
| 244 |
Arg args[5]; |
| 245 |
|
| 246 |
setbuf(stdout, NULL); |
| 247 |
setbuf(stderr, NULL); |
| 248 |
|
| 249 |
shellWidget = XtInitialize(argv[0], "XTiff", shellOptions, |
| 250 |
XtNumber(shellOptions), &argc, argv); |
| 251 |
|
| 252 |
XSetErrorHandler(XTiffErrorHandler); |
| 253 |
|
| 254 |
XtGetApplicationResources( |
| 255 |
shellWidget, &appData, (XtResourceList)clientResources, |
| 256 |
(Cardinal)XtNumber(clientResources), (ArgList)NULL, (Cardinal)0); |
| 257 |
|
| 258 |
if ((argc <= 1) || (argc > 2) || appData.help) |
| 259 |
Usage(); |
| 260 |
|
| 261 |
if (appData.verbose == False) |
| 262 |
{ |
| 263 |
TIFFSetErrorHandler(0); |
| 264 |
TIFFSetWarningHandler(0); |
| 265 |
} |
| 266 |
|
| 267 |
fileName = argv[1]; |
| 268 |
|
| 269 |
xDisplay = XtDisplay(shellWidget); |
| 270 |
xScreen = DefaultScreen(xDisplay); |
| 271 |
|
| 272 |
OpenTIFFFile(); |
| 273 |
GetTIFFHeader(); |
| 274 |
SimpleGammaCorrection(); |
| 275 |
GetVisual(); |
| 276 |
GetTIFFImage(); |
| 277 |
|
| 278 |
/* |
| 279 |
* Send visual, colormap, depth and iconPixmap to shellWidget. |
| 280 |
* Sending the visual to the shell is only possible with the advent of R4. |
| 281 |
*/ |
| 282 |
XtSetArg(args[0], XtNvisual, xVisual); |
| 283 |
XtSetArg(args[1], XtNcolormap, xColormap); |
| 284 |
XtSetArg(args[2], XtNdepth, |
| 285 |
xImageDepth == 1 ? DefaultDepth(xDisplay, xScreen) : xImageDepth); |
| 286 |
XtSetArg(args[3], XtNiconPixmap, |
| 287 |
XCreateBitmapFromData(xDisplay, RootWindow(xDisplay, xScreen), |
| 288 |
xtifficon_bits, xtifficon_width, |
| 289 |
xtifficon_height)); |
| 290 |
XtSetArg(args[4], XtNallowShellResize, True); |
| 291 |
XtSetValues(shellWidget, args, 5); |
| 292 |
|
| 293 |
/* |
| 294 |
* widget instance hierarchy |
| 295 |
*/ |
| 296 |
formWidget = XtCreateManagedWidget("form", formWidgetClass, shellWidget, |
| 297 |
formArgs, XtNumber(formArgs)); |
| 298 |
|
| 299 |
widget_list[0] = listWidget = XtCreateWidget( |
| 300 |
"list", listWidgetClass, formWidget, listArgs, XtNumber(listArgs)); |
| 301 |
|
| 302 |
widget_list[1] = labelWidget = XtCreateWidget( |
| 303 |
"label", labelWidgetClass, formWidget, labelArgs, XtNumber(labelArgs)); |
| 304 |
|
| 305 |
widget_list[2] = imageWidget = XtCreateWidget( |
| 306 |
"image", widgetClass, formWidget, imageArgs, XtNumber(imageArgs)); |
| 307 |
|
| 308 |
XtManageChildren(widget_list, XtNumber(widget_list)); |
| 309 |
|
| 310 |
/* |
| 311 |
* initial widget sizes - for small images let xtiff size itself |
| 312 |
*/ |
| 313 |
if (tfImageWidth >= appData.viewportWidth) |
| 314 |
{ |
| 315 |
XtSetArg(args[0], XtNwidth, appData.viewportWidth); |
| 316 |
XtSetValues(shellWidget, args, 1); |
| 317 |
} |
| 318 |
if (tfImageHeight >= appData.viewportHeight) |
| 319 |
{ |
| 320 |
XtSetArg(args[0], XtNheight, appData.viewportHeight); |
| 321 |
XtSetValues(shellWidget, args, 1); |
| 322 |
} |
| 323 |
|
| 324 |
XtSetArg(args[0], XtNwidth, tfImageWidth); |
| 325 |
XtSetArg(args[1], XtNheight, tfImageHeight); |
| 326 |
XtSetValues(imageWidget, args, 2); |
| 327 |
|
| 328 |
/* |
| 329 |
* formWidget uses these constraints but they are stored in the children. |
| 330 |
*/ |
| 331 |
XtSetArg(args[0], XtNfromVert, listWidget); |
| 332 |
XtSetValues(imageWidget, args, 1); |
| 333 |
XtSetArg(args[0], XtNfromHoriz, listWidget); |
| 334 |
XtSetValues(labelWidget, args, 1); |
| 335 |
|
| 336 |
SetNameLabel(); |
| 337 |
|
| 338 |
XtAddCallback(listWidget, XtNcallback, (XtCallbackProc)SelectProc, |
| 339 |
(XtPointer)NULL); |
| 340 |
|
| 341 |
XtAddActions(actionsTable, XtNumber(actionsTable)); |
| 342 |
XtSetArg(args[0], XtNtranslations, |
| 343 |
XtParseTranslationTable(translationsTable)); |
| 344 |
XtSetValues(formWidget, &args[0], 1); |
| 345 |
XtSetValues(imageWidget, &args[0], 1); |
| 346 |
|
| 347 |
/* |
| 348 |
* This is intended to be a little faster than going through |
| 349 |
* the translation manager. |
| 350 |
*/ |
| 351 |
XtAddEventHandler(imageWidget, |
| 352 |
ExposureMask | ButtonPressMask | ButtonReleaseMask | |
| 353 |
Button1MotionMask | KeyPressMask, |
| 354 |
False, EventProc, NULL); |
| 355 |
|
| 356 |
XtRealizeWidget(shellWidget); |
| 357 |
|
| 358 |
window_attributes.cursor = XCreateFontCursor(xDisplay, XC_fleur); |
| 359 |
XChangeWindowAttributes(xDisplay, XtWindow(imageWidget), CWCursor, |
| 360 |
&window_attributes); |
| 361 |
|
| 362 |
CreateXImage(); |
| 363 |
|
| 364 |
XtMainLoop(); |
| 365 |
|
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
void OpenTIFFFile() |
| 370 |
{ |
| 371 |
if (tfFile != NULL) |
| 372 |
TIFFClose(tfFile); |
| 373 |
|
| 374 |
if ((tfFile = TIFFOpen(fileName, "r")) == NULL) |
| 375 |
{ |
| 376 |
fprintf(appData.verbose ? stderr : stdout, |
| 377 |
"xtiff: can't open %s as a TIFF file\n", fileName); |
| 378 |
exit(0); |
| 379 |
} |
| 380 |
|
| 381 |
tfMultiPage = (TIFFLastDirectory(tfFile) ? False : True); |
| 382 |
} |
| 383 |
|
| 384 |
void GetTIFFHeader() |
| 385 |
{ |
| 386 |
register int i; |
| 387 |
|
| 388 |
if (!TIFFSetDirectory(tfFile, tfDirectory)) |
| 389 |
{ |
| 390 |
fprintf(stderr, "xtiff: can't seek to directory %d in %s\n", |
| 391 |
tfDirectory, fileName); |
| 392 |
exit(0); |
| 393 |
} |
| 394 |
|
| 395 |
TIFFGetField(tfFile, TIFFTAG_IMAGEWIDTH, &tfImageWidth); |
| 396 |
TIFFGetField(tfFile, TIFFTAG_IMAGELENGTH, &tfImageHeight); |
| 397 |
|
| 398 |
/* |
| 399 |
* If the following tags aren't present then use the TIFF defaults. |
| 400 |
*/ |
| 401 |
TIFFGetFieldDefaulted(tfFile, TIFFTAG_BITSPERSAMPLE, &tfBitsPerSample); |
| 402 |
TIFFGetFieldDefaulted(tfFile, TIFFTAG_SAMPLESPERPIXEL, &tfSamplesPerPixel); |
| 403 |
TIFFGetFieldDefaulted(tfFile, TIFFTAG_PLANARCONFIG, &tfPlanarConfiguration); |
| 404 |
TIFFGetFieldDefaulted(tfFile, TIFFTAG_GRAYRESPONSEUNIT, |
| 405 |
&tfGrayResponseUnit); |
| 406 |
|
| 407 |
tfUnitMap = tfGrayResponseUnitMap[tfGrayResponseUnit]; |
| 408 |
colormapSize = 1 << tfBitsPerSample; |
| 409 |
tfImageDepth = tfBitsPerSample * tfSamplesPerPixel; |
| 410 |
|
| 411 |
dRed = (double *)malloc(colormapSize * sizeof(double)); |
| 412 |
dGreen = (double *)malloc(colormapSize * sizeof(double)); |
| 413 |
dBlue = (double *)malloc(colormapSize * sizeof(double)); |
| 414 |
MCHECK(dRed); |
| 415 |
MCHECK(dGreen); |
| 416 |
MCHECK(dBlue); |
| 417 |
|
| 418 |
/* |
| 419 |
* If TIFFTAG_PHOTOMETRIC is not present then assign a reasonable default. |
| 420 |
* The TIFF 5.0 specification doesn't give a default. |
| 421 |
*/ |
| 422 |
if (!TIFFGetField(tfFile, TIFFTAG_PHOTOMETRIC, |
| 423 |
&tfPhotometricInterpretation)) |
| 424 |
{ |
| 425 |
if (tfSamplesPerPixel != 1) |
| 426 |
tfPhotometricInterpretation = PHOTOMETRIC_RGB; |
| 427 |
else if (tfBitsPerSample == 1) |
| 428 |
tfPhotometricInterpretation = PHOTOMETRIC_MINISBLACK; |
| 429 |
else if (TIFFGetField(tfFile, TIFFTAG_COLORMAP, &redMap, &greenMap, |
| 430 |
&blueMap)) |
| 431 |
{ |
| 432 |
tfPhotometricInterpretation = PHOTOMETRIC_PALETTE; |
| 433 |
redMap = greenMap = blueMap = NULL; |
| 434 |
} |
| 435 |
else |
| 436 |
tfPhotometricInterpretation = PHOTOMETRIC_MINISBLACK; |
| 437 |
} |
| 438 |
|
| 439 |
/* |
| 440 |
* Given TIFFTAG_PHOTOMETRIC extract or create the response curves. |
| 441 |
*/ |
| 442 |
switch (tfPhotometricInterpretation) |
| 443 |
{ |
| 444 |
case PHOTOMETRIC_RGB: |
| 445 |
redMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 446 |
greenMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 447 |
blueMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 448 |
MCHECK(redMap); |
| 449 |
MCHECK(greenMap); |
| 450 |
MCHECK(blueMap); |
| 451 |
for (i = 0; i < colormapSize; i++) |
| 452 |
dRed[i] = dGreen[i] = dBlue[i] = |
| 453 |
(double)SCALE(i, colormapSize - 1); |
| 454 |
break; |
| 455 |
case PHOTOMETRIC_PALETTE: |
| 456 |
if (!TIFFGetField(tfFile, TIFFTAG_COLORMAP, &redMap, &greenMap, |
| 457 |
&blueMap)) |
| 458 |
{ |
| 459 |
redMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 460 |
greenMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 461 |
blueMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 462 |
MCHECK(redMap); |
| 463 |
MCHECK(greenMap); |
| 464 |
MCHECK(blueMap); |
| 465 |
for (i = 0; i < colormapSize; i++) |
| 466 |
dRed[i] = dGreen[i] = dBlue[i] = |
| 467 |
(double)SCALE(i, colormapSize - 1); |
| 468 |
} |
| 469 |
else |
| 470 |
{ |
| 471 |
CheckAndCorrectColormap(); |
| 472 |
for (i = 0; i < colormapSize; i++) |
| 473 |
{ |
| 474 |
dRed[i] = (double)redMap[i]; |
| 475 |
dGreen[i] = (double)greenMap[i]; |
| 476 |
dBlue[i] = (double)blueMap[i]; |
| 477 |
} |
| 478 |
} |
| 479 |
break; |
| 480 |
case PHOTOMETRIC_MINISWHITE: |
| 481 |
redMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 482 |
greenMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 483 |
blueMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 484 |
MCHECK(redMap); |
| 485 |
MCHECK(greenMap); |
| 486 |
MCHECK(blueMap); |
| 487 |
for (i = 0; i < colormapSize; i++) |
| 488 |
dRed[i] = dGreen[i] = dBlue[i] = |
| 489 |
(double)SCALE(colormapSize - 1 - i, colormapSize - 1); |
| 490 |
break; |
| 491 |
case PHOTOMETRIC_MINISBLACK: |
| 492 |
redMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 493 |
greenMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 494 |
blueMap = (uint16_t *)malloc(colormapSize * sizeof(uint16_t)); |
| 495 |
MCHECK(redMap); |
| 496 |
MCHECK(greenMap); |
| 497 |
MCHECK(blueMap); |
| 498 |
for (i = 0; i < colormapSize; i++) |
| 499 |
dRed[i] = dGreen[i] = dBlue[i] = |
| 500 |
(double)SCALE(i, colormapSize - 1); |
| 501 |
break; |
| 502 |
default: |
| 503 |
fprintf(stderr, |
| 504 |
"xtiff: can't display photometric interpretation type %d\n", |
| 505 |
tfPhotometricInterpretation); |
| 506 |
exit(0); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
void SetNameLabel() |
| 511 |
{ |
| 512 |
char buffer[BUFSIZ]; |
| 513 |
Arg args[1]; |
| 514 |
|
| 515 |
if (tfMultiPage) |
| 516 |
snprintf(buffer, sizeof(buffer), "%s - page %d", fileName, tfDirectory); |
| 517 |
else |
| 518 |
snprintf(buffer, sizeof(buffer), "%s", fileName); |
| 519 |
XtSetArg(args[0], XtNlabel, buffer); |
| 520 |
XtSetValues(labelWidget, args, 1); |
| 521 |
} |
| 522 |
|
| 523 |
/* |
| 524 |
* Many programs get TIFF colormaps wrong. They use 8-bit colormaps instead of |
| 525 |
* 16-bit colormaps. This function is a heuristic to detect and correct this. |
| 526 |
*/ |
| 527 |
void CheckAndCorrectColormap() |
| 528 |
{ |
| 529 |
register int i; |
| 530 |
|
| 531 |
for (i = 0; i < colormapSize; i++) |
| 532 |
if ((redMap[i] > 255) || (greenMap[i] > 255) || (blueMap[i] > 255)) |
| 533 |
return; |
| 534 |
|
| 535 |
for (i = 0; i < colormapSize; i++) |
| 536 |
{ |
| 537 |
redMap[i] = SCALE(redMap[i], 255); |
| 538 |
greenMap[i] = SCALE(greenMap[i], 255); |
| 539 |
blueMap[i] = SCALE(blueMap[i], 255); |
| 540 |
} |
| 541 |
TIFFWarning(fileName, "Assuming 8-bit colormap"); |
| 542 |
} |
| 543 |
|
| 544 |
void SimpleGammaCorrection() |
| 545 |
{ |
| 546 |
register int i; |
| 547 |
register double i_gamma = 1.0 / appData.gamma; |
| 548 |
|
| 549 |
for (i = 0; i < colormapSize; i++) |
| 550 |
{ |
| 551 |
if (((tfPhotometricInterpretation == PHOTOMETRIC_MINISWHITE) && |
| 552 |
(i == colormapSize - 1)) || |
| 553 |
((tfPhotometricInterpretation == PHOTOMETRIC_MINISBLACK) && |
| 554 |
(i == 0))) |
| 555 |
redMap[i] = greenMap[i] = blueMap[i] = 0; |
| 556 |
else |
| 557 |
{ |
| 558 |
redMap[i] = ROUND((pow(dRed[i] / 65535.0, i_gamma) * 65535.0)); |
| 559 |
greenMap[i] = ROUND((pow(dGreen[i] / 65535.0, i_gamma) * 65535.0)); |
| 560 |
blueMap[i] = ROUND((pow(dBlue[i] / 65535.0, i_gamma) * 65535.0)); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
free(dRed); |
| 565 |
free(dGreen); |
| 566 |
free(dBlue); |
| 567 |
} |
| 568 |
|
| 569 |
static char *classNames[] = {"StaticGray", "GrayScale", "StaticColor", |
| 570 |
"PseudoColor", "TrueColor", "DirectColor"}; |
| 571 |
|
| 572 |
/* |
| 573 |
* Current limitation: the visual is set initially by the first file. |
| 574 |
* It cannot be changed. |
| 575 |
*/ |
| 576 |
void GetVisual() |
| 577 |
{ |
| 578 |
XColor *colors = NULL; |
| 579 |
unsigned long *pixels = NULL; |
| 580 |
unsigned long i; |
| 581 |
|
| 582 |
switch (tfImageDepth) |
| 583 |
{ |
| 584 |
/* |
| 585 |
* X really wants a 32-bit image with the fourth channel unused, |
| 586 |
* but the visual structure thinks it's 24-bit. bitmap_unit is 32. |
| 587 |
*/ |
| 588 |
case 32: |
| 589 |
case 24: |
| 590 |
if (SearchVisualList(24, DirectColor, &xVisual) == False) |
| 591 |
{ |
| 592 |
fprintf(stderr, |
| 593 |
"xtiff: 24-bit DirectColor visual not available\n"); |
| 594 |
exit(0); |
| 595 |
} |
| 596 |
|
| 597 |
colors = (XColor *)malloc(3 * colormapSize * sizeof(XColor)); |
| 598 |
MCHECK(colors); |
| 599 |
|
| 600 |
for (i = 0; i < colormapSize; i++) |
| 601 |
{ |
| 602 |
colors[i].pixel = (i << 16) + (i << 8) + i; |
| 603 |
colors[i].red = redMap[i]; |
| 604 |
colors[i].green = greenMap[i]; |
| 605 |
colors[i].blue = blueMap[i]; |
| 606 |
colors[i].flags = DoRed | DoGreen | DoBlue; |
| 607 |
} |
| 608 |
|
| 609 |
xColormap = XCreateColormap(xDisplay, RootWindow(xDisplay, xScreen), |
| 610 |
xVisual, AllocAll); |
| 611 |
XStoreColors(xDisplay, xColormap, colors, colormapSize); |
| 612 |
break; |
| 613 |
case 8: |
| 614 |
case 4: |
| 615 |
case 2: |
| 616 |
/* |
| 617 |
* We assume that systems with 24-bit visuals also have 8-bit |
| 618 |
* visuals. We don't promote from 8-bit PseudoColor to 24/32 bit |
| 619 |
* DirectColor. |
| 620 |
*/ |
| 621 |
switch (tfPhotometricInterpretation) |
| 622 |
{ |
| 623 |
case PHOTOMETRIC_MINISWHITE: |
| 624 |
case PHOTOMETRIC_MINISBLACK: |
| 625 |
if (SearchVisualList((int)tfImageDepth, GrayScale, |
| 626 |
&xVisual) == True) |
| 627 |
break; |
| 628 |
case PHOTOMETRIC_PALETTE: |
| 629 |
if (SearchVisualList((int)tfImageDepth, PseudoColor, |
| 630 |
&xVisual) == True) |
| 631 |
break; |
| 632 |
default: |
| 633 |
fprintf(stderr, |
| 634 |
"xtiff: Unsupported TIFF/X configuration\n"); |
| 635 |
exit(0); |
| 636 |
} |
| 637 |
|
| 638 |
colors = (XColor *)malloc(colormapSize * sizeof(XColor)); |
| 639 |
MCHECK(colors); |
| 640 |
|
| 641 |
for (i = 0; i < colormapSize; i++) |
| 642 |
{ |
| 643 |
colors[i].pixel = i; |
| 644 |
colors[i].red = redMap[i]; |
| 645 |
colors[i].green = greenMap[i]; |
| 646 |
colors[i].blue = blueMap[i]; |
| 647 |
colors[i].flags = DoRed | DoGreen | DoBlue; |
| 648 |
} |
| 649 |
|
| 650 |
/* |
| 651 |
* xtiff's colormap allocation is private. It does not attempt |
| 652 |
* to detect whether any existing colormap entries are suitable |
| 653 |
* for its use. This will cause colormap flashing. Furthermore, |
| 654 |
* background and foreground are taken from the environment. |
| 655 |
* For example, the foreground color may be red when the visual |
| 656 |
* is GrayScale. If the colormap is completely populated, |
| 657 |
* Xt will not be able to allocate fg and bg. |
| 658 |
*/ |
| 659 |
if (tfImageDepth == 8) |
| 660 |
xColormap = XCreateColormap( |
| 661 |
xDisplay, RootWindow(xDisplay, xScreen), xVisual, AllocAll); |
| 662 |
else |
| 663 |
{ |
| 664 |
xColormap = |
| 665 |
XCreateColormap(xDisplay, RootWindow(xDisplay, xScreen), |
| 666 |
xVisual, AllocNone); |
| 667 |
pixels = (unsigned long *)malloc(colormapSize * |
| 668 |
sizeof(unsigned long)); |
| 669 |
MCHECK(pixels); |
| 670 |
(void)XAllocColorCells(xDisplay, xColormap, True, NULL, 0, |
| 671 |
pixels, colormapSize); |
| 672 |
basePixel = (unsigned char)pixels[0]; |
| 673 |
free(pixels); |
| 674 |
} |
| 675 |
XStoreColors(xDisplay, xColormap, colors, colormapSize); |
| 676 |
break; |
| 677 |
case 1: |
| 678 |
xImageDepth = 1; |
| 679 |
xVisual = DefaultVisual(xDisplay, xScreen); |
| 680 |
xColormap = DefaultColormap(xDisplay, xScreen); |
| 681 |
break; |
| 682 |
default: |
| 683 |
fprintf(stderr, "xtiff: unsupported image depth %d\n", |
| 684 |
tfImageDepth); |
| 685 |
exit(0); |
| 686 |
} |
| 687 |
|
| 688 |
if (appData.verbose == True) |
| 689 |
fprintf(stderr, "%s: Using %d-bit %s visual.\n", fileName, xImageDepth, |
| 690 |
classNames[xVisual->class]); |
| 691 |
|
| 692 |
if (colors != NULL) |
| 693 |
free(colors); |
| 694 |
if (grayMap != NULL) |
| 695 |
free(grayMap); |
| 696 |
if (redMap != NULL) |
| 697 |
free(redMap); |
| 698 |
if (greenMap != NULL) |
| 699 |
free(greenMap); |
| 700 |
if (blueMap != NULL) |
| 701 |
free(blueMap); |
| 702 |
|
| 703 |
colors = NULL; |
| 704 |
grayMap = redMap = greenMap = blueMap = NULL; |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
* Search for an appropriate visual. Promote where necessary. |
| 709 |
* Check to make sure that ENOUGH colormap entries are writeable. |
| 710 |
* basePixel was determined when XAllocColorCells() contiguously |
| 711 |
* allocated enough entries. basePixel is used below in GetTIFFImage. |
| 712 |
*/ |
| 713 |
Boolean SearchVisualList(image_depth, visual_class, visual) |
| 714 |
int image_depth, visual_class; |
| 715 |
Visual **visual; |
| 716 |
{ |
| 717 |
XVisualInfo template_visual, *visual_list, *vl; |
| 718 |
int i, n_visuals; |
| 719 |
|
| 720 |
template_visual.screen = xScreen; |
| 721 |
vl = visual_list = XGetVisualInfo(xDisplay, VisualScreenMask, |
| 722 |
&template_visual, &n_visuals); |
| 723 |
|
| 724 |
if (n_visuals == 0) |
| 725 |
{ |
| 726 |
fprintf(stderr, "xtiff: visual list not available\n"); |
| 727 |
exit(0); |
| 728 |
} |
| 729 |
|
| 730 |
for (i = 0; i < n_visuals; vl++, i++) |
| 731 |
{ |
| 732 |
if ((vl->class == visual_class) && (vl->depth >= image_depth) && |
| 733 |
(vl->visual->map_entries >= (1 << vl->depth))) |
| 734 |
{ |
| 735 |
*visual = vl->visual; |
| 736 |
xImageDepth = vl->depth; |
| 737 |
xRedMask = vl->red_mask; |
| 738 |
xGreenMask = vl->green_mask; |
| 739 |
xBlueMask = vl->blue_mask; |
| 740 |
XFree((char *)visual_list); |
| 741 |
return True; |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
XFree((char *)visual_list); |
| 746 |
return False; |
| 747 |
} |
| 748 |
|
| 749 |
void GetTIFFImage() |
| 750 |
{ |
| 751 |
int pixel_map[3], red_shift, green_shift, blue_shift; |
| 752 |
char *scan_line, *output_p, *input_p; |
| 753 |
uint32_t i, j; |
| 754 |
uint16_t s; |
| 755 |
|
| 756 |
scan_line = (char *)malloc(tfBytesPerRow = TIFFScanlineSize(tfFile)); |
| 757 |
MCHECK(scan_line); |
| 758 |
|
| 759 |
if ((tfImageDepth == 32) || (tfImageDepth == 24)) |
| 760 |
{ |
| 761 |
output_p = imageMemory = |
| 762 |
(char *)malloc(tfImageWidth * tfImageHeight * 4); |
| 763 |
MCHECK(imageMemory); |
| 764 |
|
| 765 |
/* |
| 766 |
* Handle different color masks for different frame buffers. |
| 767 |
*/ |
| 768 |
if (ImageByteOrder(xDisplay) == LSBFirst) |
| 769 |
{ /* DECstation 5000 */ |
| 770 |
red_shift = pixel_map[0] = |
| 771 |
xRedMask == 0xFF000000 |
| 772 |
? 3 |
| 773 |
: (xRedMask == 0xFF0000 ? 2 : (xRedMask == 0xFF00 ? 1 : 0)); |
| 774 |
green_shift = pixel_map[1] = |
| 775 |
xGreenMask == 0xFF000000 |
| 776 |
? 3 |
| 777 |
: (xGreenMask == 0xFF0000 ? 2 |
| 778 |
: (xGreenMask == 0xFF00 ? 1 : 0)); |
| 779 |
blue_shift = pixel_map[2] = |
| 780 |
xBlueMask == 0xFF000000 |
| 781 |
? 3 |
| 782 |
: (xBlueMask == 0xFF0000 ? 2 |
| 783 |
: (xBlueMask == 0xFF00 ? 1 : 0)); |
| 784 |
} |
| 785 |
else |
| 786 |
{ /* Ardent */ |
| 787 |
red_shift = pixel_map[0] = |
| 788 |
xRedMask == 0xFF000000 |
| 789 |
? 0 |
| 790 |
: (xRedMask == 0xFF0000 ? 1 : (xRedMask == 0xFF00 ? 2 : 3)); |
| 791 |
green_shift = pixel_map[0] = |
| 792 |
xGreenMask == 0xFF000000 |
| 793 |
? 0 |
| 794 |
: (xGreenMask == 0xFF0000 ? 1 |
| 795 |
: (xGreenMask == 0xFF00 ? 2 : 3)); |
| 796 |
blue_shift = pixel_map[0] = |
| 797 |
xBlueMask == 0xFF000000 |
| 798 |
? 0 |
| 799 |
: (xBlueMask == 0xFF0000 ? 1 |
| 800 |
: (xBlueMask == 0xFF00 ? 2 : 3)); |
| 801 |
} |
| 802 |
|
| 803 |
if (tfPlanarConfiguration == PLANARCONFIG_CONTIG) |
| 804 |
{ |
| 805 |
for (i = 0; i < tfImageHeight; i++) |
| 806 |
{ |
| 807 |
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0) |
| 808 |
break; |
| 809 |
for (input_p = scan_line, j = 0; j < tfImageWidth; j++) |
| 810 |
{ |
| 811 |
*(output_p + red_shift) = *input_p++; |
| 812 |
*(output_p + green_shift) = *input_p++; |
| 813 |
*(output_p + blue_shift) = *input_p++; |
| 814 |
output_p += 4; |
| 815 |
if (tfSamplesPerPixel == 4) /* skip the fourth channel */ |
| 816 |
input_p++; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
else |
| 821 |
{ |
| 822 |
for (s = 0; s < tfSamplesPerPixel; s++) |
| 823 |
{ |
| 824 |
if (s == 3) /* skip the fourth channel */ |
| 825 |
continue; |
| 826 |
for (i = 0; i < tfImageHeight; i++) |
| 827 |
{ |
| 828 |
if (TIFFReadScanline(tfFile, scan_line, i, s) < 0) |
| 829 |
break; |
| 830 |
input_p = scan_line; |
| 831 |
output_p = |
| 832 |
imageMemory + (i * tfImageWidth * 4) + pixel_map[s]; |
| 833 |
for (j = 0; j < tfImageWidth; j++, output_p += 4) |
| 834 |
*output_p = *input_p++; |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
else |
| 840 |
{ |
| 841 |
if (xImageDepth == tfImageDepth) |
| 842 |
{ |
| 843 |
output_p = imageMemory = |
| 844 |
(char *)malloc(tfBytesPerRow * tfImageHeight); |
| 845 |
MCHECK(imageMemory); |
| 846 |
|
| 847 |
for (i = 0; i < tfImageHeight; i++, output_p += tfBytesPerRow) |
| 848 |
if (TIFFReadScanline(tfFile, output_p, i, 0) < 0) |
| 849 |
break; |
| 850 |
} |
| 851 |
else if ((xImageDepth == 8) && (tfImageDepth == 4)) |
| 852 |
{ |
| 853 |
output_p = imageMemory = |
| 854 |
(char *)malloc(tfBytesPerRow * 2 * tfImageHeight + 2); |
| 855 |
MCHECK(imageMemory); |
| 856 |
|
| 857 |
/* |
| 858 |
* If a scanline is of odd size the inner loop below will overshoot. |
| 859 |
* This is handled very simply by recalculating the start point at |
| 860 |
* each scanline and padding imageMemory a little at the end. |
| 861 |
*/ |
| 862 |
for (i = 0; i < tfImageHeight; i++) |
| 863 |
{ |
| 864 |
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0) |
| 865 |
break; |
| 866 |
output_p = &imageMemory[i * tfImageWidth]; |
| 867 |
input_p = scan_line; |
| 868 |
for (j = 0; j < tfImageWidth; j += 2, input_p++) |
| 869 |
{ |
| 870 |
*output_p++ = (*input_p >> 4) + basePixel; |
| 871 |
*output_p++ = (*input_p & 0xf) + basePixel; |
| 872 |
} |
| 873 |
} |
| 874 |
} |
| 875 |
else if ((xImageDepth == 8) && (tfImageDepth == 2)) |
| 876 |
{ |
| 877 |
output_p = imageMemory = |
| 878 |
(char *)malloc(tfBytesPerRow * 4 * tfImageHeight + 4); |
| 879 |
MCHECK(imageMemory); |
| 880 |
|
| 881 |
for (i = 0; i < tfImageHeight; i++) |
| 882 |
{ |
| 883 |
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0) |
| 884 |
break; |
| 885 |
output_p = &imageMemory[i * tfImageWidth]; |
| 886 |
input_p = scan_line; |
| 887 |
for (j = 0; j < tfImageWidth; j += 4, input_p++) |
| 888 |
{ |
| 889 |
*output_p++ = (*input_p >> 6) + basePixel; |
| 890 |
*output_p++ = ((*input_p >> 4) & 3) + basePixel; |
| 891 |
*output_p++ = ((*input_p >> 2) & 3) + basePixel; |
| 892 |
*output_p++ = (*input_p & 3) + basePixel; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
else if ((xImageDepth == 4) && (tfImageDepth == 2)) |
| 897 |
{ |
| 898 |
output_p = imageMemory = |
| 899 |
(char *)malloc(tfBytesPerRow * 2 * tfImageHeight + 2); |
| 900 |
MCHECK(imageMemory); |
| 901 |
|
| 902 |
for (i = 0; i < tfImageHeight; i++) |
| 903 |
{ |
| 904 |
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0) |
| 905 |
break; |
| 906 |
output_p = &imageMemory[i * tfBytesPerRow * 2]; |
| 907 |
input_p = scan_line; |
| 908 |
for (j = 0; j < tfImageWidth; j += 4, input_p++) |
| 909 |
{ |
| 910 |
*output_p++ = |
| 911 |
(((*input_p >> 6) << 4) | ((*input_p >> 4) & 3)) + |
| 912 |
basePixel; |
| 913 |
*output_p++ = |
| 914 |
((((*input_p >> 2) & 3) << 4) | (*input_p & 3)) + |
| 915 |
basePixel; |
| 916 |
} |
| 917 |
} |
| 918 |
} |
| 919 |
else |
| 920 |
{ |
| 921 |
fprintf( |
| 922 |
stderr, |
| 923 |
"xtiff: can't handle %d-bit TIFF file on an %d-bit display\n", |
| 924 |
tfImageDepth, xImageDepth); |
| 925 |
exit(0); |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
free(scan_line); |
| 930 |
} |
| 931 |
|
| 932 |
void CreateXImage() |
| 933 |
{ |
| 934 |
XGCValues gc_values; |
| 935 |
GC bitmap_gc; |
| 936 |
|
| 937 |
xOffset = yOffset = 0; |
| 938 |
grabX = grabY = -1; |
| 939 |
|
| 940 |
xImage = XCreateImage(xDisplay, xVisual, xImageDepth, |
| 941 |
xImageDepth == 1 ? XYBitmap : ZPixmap, /* offset */ 0, |
| 942 |
(char *)imageMemory, tfImageWidth, tfImageHeight, |
| 943 |
/* bitmap_pad */ 8, /* bytes_per_line */ 0); |
| 944 |
|
| 945 |
/* |
| 946 |
* libtiff converts LSB data into MSB but doesn't change the FillOrder tag. |
| 947 |
*/ |
| 948 |
if (xImageDepth == 1) |
| 949 |
xImage->bitmap_bit_order = MSBFirst; |
| 950 |
if (xImageDepth <= 8) |
| 951 |
xImage->byte_order = MSBFirst; |
| 952 |
|
| 953 |
/* |
| 954 |
* create an appropriate GC |
| 955 |
*/ |
| 956 |
gc_values.function = GXcopy; |
| 957 |
gc_values.plane_mask = AllPlanes; |
| 958 |
if (tfPhotometricInterpretation == PHOTOMETRIC_MINISBLACK) |
| 959 |
{ |
| 960 |
gc_values.foreground = XWhitePixel(xDisplay, xScreen); |
| 961 |
gc_values.background = XBlackPixel(xDisplay, xScreen); |
| 962 |
} |
| 963 |
else |
| 964 |
{ |
| 965 |
gc_values.foreground = XBlackPixel(xDisplay, xScreen); |
| 966 |
gc_values.background = XWhitePixel(xDisplay, xScreen); |
| 967 |
} |
| 968 |
xWinGc = XCreateGC(xDisplay, XtWindow(shellWidget), |
| 969 |
GCFunction | GCPlaneMask | GCForeground | GCBackground, |
| 970 |
&gc_values); |
| 971 |
|
| 972 |
/* |
| 973 |
* create the pixmap and load the image |
| 974 |
*/ |
| 975 |
if (appData.usePixmap == True) |
| 976 |
{ |
| 977 |
xImagePixmap = |
| 978 |
XCreatePixmap(xDisplay, RootWindow(xDisplay, xScreen), |
| 979 |
xImage->width, xImage->height, xImageDepth); |
| 980 |
|
| 981 |
/* |
| 982 |
* According to the O'Reilly X Protocol Reference Manual, page 53, |
| 983 |
* "A pixmap depth of one is always supported and listed, but windows |
| 984 |
* of depth one might not be supported." Therefore we create a pixmap |
| 985 |
* of depth one and use XCopyPlane(). This is idiomatic. |
| 986 |
*/ |
| 987 |
if (xImageDepth == 1) |
| 988 |
{ /* just pass the bits through */ |
| 989 |
gc_values.foreground = 1; /* foreground describes set bits */ |
| 990 |
gc_values.background = 0; /* background describes clear bits */ |
| 991 |
bitmap_gc = XCreateGC(xDisplay, xImagePixmap, |
| 992 |
GCForeground | GCBackground, &gc_values); |
| 993 |
XPutImage(xDisplay, xImagePixmap, bitmap_gc, xImage, 0, 0, 0, 0, |
| 994 |
xImage->width, xImage->height); |
| 995 |
} |
| 996 |
else |
| 997 |
XPutImage(xDisplay, xImagePixmap, xWinGc, xImage, 0, 0, 0, 0, |
| 998 |
xImage->width, xImage->height); |
| 999 |
XDestroyImage(xImage); |
| 1000 |
free(imageMemory); |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
XtCallbackProc SelectProc(w, unused_1, unused_2) |
| 1005 |
Widget w; |
| 1006 |
caddr_t unused_1; |
| 1007 |
caddr_t unused_2; |
| 1008 |
{ |
| 1009 |
XawListReturnStruct *list_return; |
| 1010 |
|
| 1011 |
list_return = XawListShowCurrent(w); |
| 1012 |
|
| 1013 |
switch (list_return->list_index) |
| 1014 |
{ |
| 1015 |
case ButtonQuit: |
| 1016 |
QuitProc(); |
| 1017 |
break; |
| 1018 |
case ButtonPreviousPage: |
| 1019 |
PreviousProc(); |
| 1020 |
break; |
| 1021 |
case ButtonNextPage: |
| 1022 |
NextProc(); |
| 1023 |
break; |
| 1024 |
default: |
| 1025 |
fprintf(stderr, "error in SelectProc\n"); |
| 1026 |
exit(0); |
| 1027 |
} |
| 1028 |
XawListUnhighlight(w); |
| 1029 |
} |
| 1030 |
|
| 1031 |
void QuitProc(void) { exit(0); } |
| 1032 |
|
| 1033 |
void NextProc() { PageProc(ButtonNextPage); } |
| 1034 |
|
| 1035 |
void PreviousProc() { PageProc(ButtonPreviousPage); } |
| 1036 |
|
| 1037 |
void PageProc(direction) int direction; |
| 1038 |
{ |
| 1039 |
XEvent fake_event; |
| 1040 |
Arg args[4]; |
| 1041 |
|
| 1042 |
switch (direction) |
| 1043 |
{ |
| 1044 |
case ButtonPreviousPage: |
| 1045 |
if (tfDirectory > 0) |
| 1046 |
TIFFSetDirectory(tfFile, --tfDirectory); |
| 1047 |
else |
| 1048 |
return; |
| 1049 |
break; |
| 1050 |
case ButtonNextPage: |
| 1051 |
if (TIFFReadDirectory(tfFile) == True) |
| 1052 |
tfDirectory++; |
| 1053 |
else |
| 1054 |
return; |
| 1055 |
break; |
| 1056 |
default: |
| 1057 |
fprintf(stderr, "error in PageProc\n"); |
| 1058 |
exit(0); |
| 1059 |
} |
| 1060 |
|
| 1061 |
xOffset = yOffset = 0; |
| 1062 |
grabX = grabY = -1; |
| 1063 |
|
| 1064 |
GetTIFFHeader(); |
| 1065 |
SetNameLabel(); |
| 1066 |
GetTIFFImage(); |
| 1067 |
|
| 1068 |
if (appData.usePixmap == True) |
| 1069 |
XFreePixmap(xDisplay, xImagePixmap); |
| 1070 |
else |
| 1071 |
XDestroyImage(xImage); |
| 1072 |
|
| 1073 |
CreateXImage(); |
| 1074 |
|
| 1075 |
/* |
| 1076 |
* Using XtSetValues() to set the widget size causes a resize. |
| 1077 |
* This resize gets propagated up to the parent shell. |
| 1078 |
* In order to disable this visually disconcerting effect, |
| 1079 |
* shell resizing is temporarily disabled. |
| 1080 |
*/ |
| 1081 |
XtSetArg(args[0], XtNallowShellResize, False); |
| 1082 |
XtSetValues(shellWidget, args, 1); |
| 1083 |
|
| 1084 |
XtSetArg(args[0], XtNwidth, tfImageWidth); |
| 1085 |
XtSetArg(args[1], XtNheight, tfImageHeight); |
| 1086 |
XtSetValues(imageWidget, args, 2); |
| 1087 |
|
| 1088 |
XtSetArg(args[0], XtNallowShellResize, True); |
| 1089 |
XtSetValues(shellWidget, args, 1); |
| 1090 |
|
| 1091 |
XClearWindow(xDisplay, XtWindow(imageWidget)); |
| 1092 |
|
| 1093 |
fake_event.type = Expose; |
| 1094 |
fake_event.xexpose.x = fake_event.xexpose.y = 0; |
| 1095 |
fake_event.xexpose.width = tfImageWidth; /* the window will clip */ |
| 1096 |
fake_event.xexpose.height = tfImageHeight; |
| 1097 |
EventProc(imageWidget, NULL, &fake_event); |
| 1098 |
} |
| 1099 |
|
| 1100 |
void EventProc(widget, unused, event) Widget widget; |
| 1101 |
caddr_t unused; |
| 1102 |
XEvent *event; |
| 1103 |
{ |
| 1104 |
int ih, iw, ww, wh, sx, sy, w, h, dx, dy; |
| 1105 |
Dimension w_width, w_height; |
| 1106 |
XEvent next_event; |
| 1107 |
Arg args[2]; |
| 1108 |
|
| 1109 |
if (event->type == MappingNotify) |
| 1110 |
{ |
| 1111 |
XRefreshKeyboardMapping((XMappingEvent *)event); |
| 1112 |
return; |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (!XtIsRealized(widget)) |
| 1116 |
return; |
| 1117 |
|
| 1118 |
if ((event->type == ButtonPress) || (event->type == ButtonRelease)) |
| 1119 |
if (event->xbutton.button != Button1) |
| 1120 |
return; |
| 1121 |
|
| 1122 |
iw = tfImageWidth; /* avoid sign problems */ |
| 1123 |
ih = tfImageHeight; |
| 1124 |
|
| 1125 |
/* |
| 1126 |
* The grabX and grabY variables record where the user grabbed the image. |
| 1127 |
* They also record whether the mouse button is down or not. |
| 1128 |
*/ |
| 1129 |
if (event->type == ButtonPress) |
| 1130 |
{ |
| 1131 |
grabX = event->xbutton.x; |
| 1132 |
grabY = event->xbutton.y; |
| 1133 |
return; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* |
| 1137 |
* imageWidget is a Core widget and doesn't get resized. |
| 1138 |
* So we calculate the size of its viewport here. |
| 1139 |
*/ |
| 1140 |
XtSetArg(args[0], XtNwidth, &w_width); |
| 1141 |
XtSetArg(args[1], XtNheight, &w_height); |
| 1142 |
XtGetValues(shellWidget, args, 2); |
| 1143 |
ww = w_width; |
| 1144 |
wh = w_height; |
| 1145 |
XtGetValues(listWidget, args, 2); |
| 1146 |
wh -= w_height; |
| 1147 |
|
| 1148 |
switch (event->type) |
| 1149 |
{ |
| 1150 |
case Expose: |
| 1151 |
dx = event->xexpose.x; |
| 1152 |
dy = event->xexpose.y; |
| 1153 |
sx = dx + xOffset; |
| 1154 |
sy = dy + yOffset; |
| 1155 |
w = MIN(event->xexpose.width, iw); |
| 1156 |
h = MIN(event->xexpose.height, ih); |
| 1157 |
break; |
| 1158 |
case KeyPress: |
| 1159 |
if ((grabX >= 0) || (grabY >= 0)) /* Mouse button is still down */ |
| 1160 |
return; |
| 1161 |
switch (XLookupKeysym((XKeyEvent *)event, /* KeySyms index */ 0)) |
| 1162 |
{ |
| 1163 |
case XK_Up: |
| 1164 |
if (ih < |
| 1165 |
wh) /* Don't scroll if the window fits the image. */ |
| 1166 |
return; |
| 1167 |
sy = yOffset + appData.translate; |
| 1168 |
sy = MIN(ih - wh, sy); |
| 1169 |
if (sy == |
| 1170 |
yOffset) /* Filter redundant stationary refreshes. */ |
| 1171 |
return; |
| 1172 |
yOffset = sy; |
| 1173 |
sx = xOffset; |
| 1174 |
dx = dy = 0; |
| 1175 |
w = ww; |
| 1176 |
h = wh; |
| 1177 |
break; |
| 1178 |
case XK_Down: |
| 1179 |
if (ih < wh) |
| 1180 |
return; |
| 1181 |
sy = yOffset - appData.translate; |
| 1182 |
sy = MAX(sy, 0); |
| 1183 |
if (sy == yOffset) |
| 1184 |
return; |
| 1185 |
yOffset = sy; |
| 1186 |
sx = xOffset; |
| 1187 |
dx = dy = 0; |
| 1188 |
w = ww; |
| 1189 |
h = wh; |
| 1190 |
break; |
| 1191 |
case XK_Left: |
| 1192 |
if (iw < ww) |
| 1193 |
return; |
| 1194 |
sx = xOffset + appData.translate; |
| 1195 |
sx = MIN(iw - ww, sx); |
| 1196 |
if (sx == xOffset) |
| 1197 |
return; |
| 1198 |
xOffset = sx; |
| 1199 |
sy = yOffset; |
| 1200 |
dx = dy = 0; |
| 1201 |
w = ww; |
| 1202 |
h = wh; |
| 1203 |
break; |
| 1204 |
case XK_Right: |
| 1205 |
if (iw < ww) |
| 1206 |
return; |
| 1207 |
sx = xOffset - appData.translate; |
| 1208 |
sx = MAX(sx, 0); |
| 1209 |
if (sx == xOffset) |
| 1210 |
return; |
| 1211 |
xOffset = sx; |
| 1212 |
sy = yOffset; |
| 1213 |
dx = dy = 0; |
| 1214 |
w = ww; |
| 1215 |
h = wh; |
| 1216 |
break; |
| 1217 |
default: |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
break; |
| 1221 |
case MotionNotify: |
| 1222 |
/* |
| 1223 |
* MotionEvent compression. Ignore multiple motion events. |
| 1224 |
* Ignore motion events if the mouse button is up. |
| 1225 |
*/ |
| 1226 |
if (XPending(xDisplay)) /* Xlib doesn't flush the output buffer */ |
| 1227 |
if (XtPeekEvent(&next_event)) |
| 1228 |
if (next_event.type == MotionNotify) |
| 1229 |
return; |
| 1230 |
if ((grabX < 0) || (grabY < 0)) |
| 1231 |
return; |
| 1232 |
sx = xOffset + grabX - (int)event->xmotion.x; |
| 1233 |
if (sx >= (iw - ww)) /* clamp x motion but allow y motion */ |
| 1234 |
sx = iw - ww; |
| 1235 |
sx = MAX(sx, 0); |
| 1236 |
sy = yOffset + grabY - (int)event->xmotion.y; |
| 1237 |
if (sy >= (ih - wh)) /* clamp y motion but allow x motion */ |
| 1238 |
sy = ih - wh; |
| 1239 |
sy = MAX(sy, 0); |
| 1240 |
if ((sx == xOffset) && (sy == yOffset)) |
| 1241 |
return; |
| 1242 |
dx = dy = 0; |
| 1243 |
w = ww; |
| 1244 |
h = wh; |
| 1245 |
break; |
| 1246 |
case ButtonRelease: |
| 1247 |
xOffset = xOffset + grabX - (int)event->xbutton.x; |
| 1248 |
xOffset = MIN(iw - ww, xOffset); |
| 1249 |
xOffset = MAX(xOffset, 0); |
| 1250 |
yOffset = yOffset + grabY - (int)event->xbutton.y; |
| 1251 |
yOffset = MIN(ih - wh, yOffset); |
| 1252 |
yOffset = MAX(yOffset, 0); |
| 1253 |
grabX = grabY = -1; |
| 1254 |
default: |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (appData.usePixmap == True) |
| 1259 |
{ |
| 1260 |
if (xImageDepth == 1) |
| 1261 |
XCopyPlane(xDisplay, xImagePixmap, XtWindow(widget), xWinGc, sx, sy, |
| 1262 |
w, h, dx, dy, 1); |
| 1263 |
else |
| 1264 |
XCopyArea(xDisplay, xImagePixmap, XtWindow(widget), xWinGc, sx, sy, |
| 1265 |
w, h, dx, dy); |
| 1266 |
} |
| 1267 |
else |
| 1268 |
XPutImage(xDisplay, XtWindow(widget), xWinGc, xImage, sx, sy, dx, dy, w, |
| 1269 |
h); |
| 1270 |
} |
| 1271 |
|
| 1272 |
void ResizeProc() |
| 1273 |
{ |
| 1274 |
Dimension w_width, w_height; |
| 1275 |
int xo, yo, ww, wh; |
| 1276 |
XEvent fake_event; |
| 1277 |
Arg args[2]; |
| 1278 |
|
| 1279 |
if ((xOffset == 0) && (yOffset == 0)) |
| 1280 |
return; |
| 1281 |
|
| 1282 |
XtSetArg(args[0], XtNwidth, &w_width); |
| 1283 |
XtSetArg(args[1], XtNheight, &w_height); |
| 1284 |
XtGetValues(shellWidget, args, 2); |
| 1285 |
ww = w_width; |
| 1286 |
wh = w_height; |
| 1287 |
XtGetValues(listWidget, args, 2); |
| 1288 |
wh -= w_height; |
| 1289 |
|
| 1290 |
xo = xOffset; |
| 1291 |
yo = yOffset; |
| 1292 |
|
| 1293 |
if ((xOffset + ww) >= tfImageWidth) |
| 1294 |
xOffset = MAX((int)tfImageWidth - ww, 0); |
| 1295 |
if ((yOffset + wh) >= tfImageHeight) |
| 1296 |
yOffset = MAX((int)tfImageHeight - wh, 0); |
| 1297 |
|
| 1298 |
/* |
| 1299 |
* Send an ExposeEvent if the origin changed. |
| 1300 |
* We have to do this because of the use and semantics of bit gravity. |
| 1301 |
*/ |
| 1302 |
if ((xo != xOffset) || (yo != yOffset)) |
| 1303 |
{ |
| 1304 |
fake_event.type = Expose; |
| 1305 |
fake_event.xexpose.x = fake_event.xexpose.y = 0; |
| 1306 |
fake_event.xexpose.width = tfImageWidth; |
| 1307 |
fake_event.xexpose.height = tfImageHeight; |
| 1308 |
EventProc(imageWidget, NULL, &fake_event); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
int XTiffErrorHandler(display, error_event) |
| 1313 |
Display *display; |
| 1314 |
XErrorEvent *error_event; |
| 1315 |
{ |
| 1316 |
char message[80]; |
| 1317 |
|
| 1318 |
/* |
| 1319 |
* Some X servers limit the size of pixmaps. |
| 1320 |
*/ |
| 1321 |
if ((error_event->error_code == BadAlloc) && |
| 1322 |
(error_event->request_code == X_CreatePixmap)) |
| 1323 |
fprintf(stderr, "xtiff: requested pixmap too big for display\n"); |
| 1324 |
else |
| 1325 |
{ |
| 1326 |
XGetErrorText(display, error_event->error_code, message, 80); |
| 1327 |
fprintf(stderr, "xtiff: error code %s\n", message); |
| 1328 |
} |
| 1329 |
|
| 1330 |
exit(0); |
| 1331 |
} |
| 1332 |
|
| 1333 |
void Usage() |
| 1334 |
{ |
| 1335 |
fprintf(stderr, "Usage xtiff: [options] tiff-file\n"); |
| 1336 |
fprintf(stderr, "\tstandard Xt options\n"); |
| 1337 |
fprintf(stderr, "\t[-help]\n"); |
| 1338 |
fprintf(stderr, "\t[-gamma gamma]\n"); |
| 1339 |
fprintf(stderr, "\t[-usePixmap (True | False)]\n"); |
| 1340 |
fprintf(stderr, "\t[-viewportWidth pixels]\n"); |
| 1341 |
fprintf(stderr, "\t[-viewportHeight pixels]\n"); |
| 1342 |
fprintf(stderr, "\t[-translate pixels]\n"); |
| 1343 |
fprintf(stderr, "\t[-verbose (True | False)]\n"); |
| 1344 |
exit(0); |
| 1345 |
} |