Skip to content

Latest commit

 

History

History
1290 lines (1150 loc) · 41 KB

File metadata and controls

1290 lines (1150 loc) · 41 KB
 
Nov 10, 2019
Nov 10, 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: xtiff.c,v 1.3 2010-06-08 18:55:15 bfriesen Exp $
*
* xtiff - view a TIFF file in an X window
*
* Dan Sears
* Chris Sears
*
* Copyright 1991 by Digital Equipment Corporation, Maynard, Massachusetts.
*
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital not be
* used in advertising or publicity pertaining to distribution of the
* software without specific, written prior permission.
*
* DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
* DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
* ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*
* Revision 1.0 90/05/07
* Initial release.
* Revision 2.0 90/12/20
* Converted to use the Athena Widgets and the Xt Intrinsics.
*
* Notes:
*
* According to the TIFF 5.0 Specification, it is possible to have
* both a TIFFTAG_COLORMAP and a TIFFTAG_COLORRESPONSECURVE. This
* doesn't make sense since a TIFFTAG_COLORMAP is 16 bits wide and
* a TIFFTAG_COLORRESPONSECURVE is tfBitsPerSample bits wide for each
* channel. This is probably a bug in the specification.
* In this case, TIFFTAG_COLORRESPONSECURVE is ignored.
* This might make sense if TIFFTAG_COLORMAP was 8 bits wide.
*
* TIFFTAG_COLORMAP is often incorrectly written as ranging from
* 0 to 255 rather than from 0 to 65535. CheckAndCorrectColormap()
* takes care of this.
*
* Only ORIENTATION_TOPLEFT is supported correctly. This is the
* default TIFF and X orientation. Other orientations will be
* displayed incorrectly.
*
* There is no support for or use of 3/3/2 DirectColor visuals.
* TIFFTAG_MINSAMPLEVALUE and TIFFTAG_MAXSAMPLEVALUE are not supported.
*
* Only TIFFTAG_BITSPERSAMPLE values that are 1, 2, 4 or 8 are supported.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <tiffio.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xproto.h>
#include <X11/Shell.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/List.h>
#include <X11/Xaw/Label.h>
#include <X11/cursorfont.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include "xtifficon.h"
#define TIFF_GAMMA "2.2" /* default gamma from the TIFF 5.0 spec */
#define ROUND(x) (uint16) ((x) + 0.5)
#define SCALE(x, s) (((x) * 65535L) / (s))
#define MCHECK(m) if (!m) { fprintf(stderr, "malloc failed\n"); exit(0); }
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define VIEWPORT_WIDTH 700
#define VIEWPORT_HEIGHT 500
#define KEY_TRANSLATE 20
#ifdef __STDC__
#define PP(args) args
#else
#define PP(args) ()
#endif
int main PP((int argc, char **argv));
void OpenTIFFFile PP((void));
void GetTIFFHeader PP((void));
void SetNameLabel PP((void));
void CheckAndCorrectColormap PP((void));
void SimpleGammaCorrection PP((void));
void GetVisual PP((void));
Boolean SearchVisualList PP((int image_depth,
int visual_class, Visual **visual));
void GetTIFFImage PP((void));
void CreateXImage PP((void));
XtCallbackProc SelectProc PP((Widget w, caddr_t unused_1, caddr_t unused_2));
void QuitProc PP((void));
void NextProc PP((void));
void PreviousProc PP((void));
void PageProc PP((int direction));
void EventProc PP((Widget widget, caddr_t unused, XEvent *event));
void ResizeProc PP((void));
int XTiffErrorHandler PP((Display *display, XErrorEvent *error_event));
void Usage PP((void));
int xtVersion = XtSpecificationRelease; /* xtiff depends on R4 or higher */
/*
* Xt data structures
*/
Widget shellWidget, formWidget, listWidget, labelWidget, imageWidget;
enum { ButtonQuit = 0, ButtonPreviousPage = 1, ButtonNextPage = 2 };
String buttonStrings[] = { "Quit", "Previous", "Next" };
static XrmOptionDescRec shellOptions[] = {
{ "-help", "*help", XrmoptionNoArg, (caddr_t) "True" },
{ "-gamma", "*gamma", XrmoptionSepArg, NULL },
{ "-usePixmap", "*usePixmap", XrmoptionSepArg, NULL },
{ "-viewportWidth", "*viewportWidth", XrmoptionSepArg, NULL },
{ "-viewportHeight", "*viewportHeight", XrmoptionSepArg, NULL },
{ "-translate", "*translate", XrmoptionSepArg, NULL },
{ "-verbose", "*verbose", XrmoptionSepArg, NULL }
};
typedef struct {
Boolean help;
float gamma;
Boolean usePixmap;
uint32 viewportWidth;
uint32 viewportHeight;
int translate;
Boolean verbose;
} AppData, *AppDataPtr;
AppData appData;
XtResource clientResources[] = {
{
"help", XtCBoolean, XtRBoolean, sizeof(Boolean),
XtOffset(AppDataPtr, help), XtRImmediate, (XtPointer) False
}, {
"gamma", "Gamma", XtRFloat, sizeof(float),
XtOffset(AppDataPtr, gamma), XtRString, (XtPointer) TIFF_GAMMA
}, {
"usePixmap", "UsePixmap", XtRBoolean, sizeof(Boolean),
XtOffset(AppDataPtr, usePixmap), XtRImmediate, (XtPointer) True
}, {
"viewportWidth", "ViewportWidth", XtRInt, sizeof(int),
XtOffset(AppDataPtr, viewportWidth), XtRImmediate,
(XtPointer) VIEWPORT_WIDTH
}, {
"viewportHeight", "ViewportHeight", XtRInt, sizeof(int),
XtOffset(AppDataPtr, viewportHeight), XtRImmediate,
(XtPointer) VIEWPORT_HEIGHT
}, {
"translate", "Translate", XtRInt, sizeof(int),
XtOffset(AppDataPtr, translate), XtRImmediate, (XtPointer) KEY_TRANSLATE
}, {
"verbose", "Verbose", XtRBoolean, sizeof(Boolean),
XtOffset(AppDataPtr, verbose), XtRImmediate, (XtPointer) True
}
};
Arg formArgs[] = {
{ XtNresizable, True }
};
Arg listArgs[] = {
{ XtNresizable, False },
{ XtNborderWidth, 0 },
{ XtNdefaultColumns, 3 },
{ XtNforceColumns, True },
{ XtNlist, (int) buttonStrings },
{ XtNnumberStrings, XtNumber(buttonStrings) },
{ XtNtop, XtChainTop },
{ XtNleft, XtChainLeft },
{ XtNbottom, XtChainTop },
{ XtNright, XtChainLeft }
};
Arg labelArgs[] = {
{ XtNresizable, False },
{ XtNwidth, 200 },
{ XtNborderWidth, 0 },
{ XtNjustify, XtJustifyLeft },
{ XtNtop, XtChainTop },
{ XtNleft, XtChainLeft },
{ XtNbottom, XtChainTop },
{ XtNright, XtChainLeft }
};
Arg imageArgs[] = {
{ XtNresizable, True },
{ XtNborderWidth, 0 },
{ XtNtop, XtChainTop },
{ XtNleft, XtChainLeft },
{ XtNbottom, XtChainTop },
{ XtNright, XtChainLeft }
};
XtActionsRec actionsTable[] = {
{ "quit", QuitProc },
{ "next", NextProc },
{ "previous", PreviousProc },
{ "notifyresize", ResizeProc }
};
char translationsTable[] = "<Key>q: quit() \n \
<Key>Q: quit() \n \
<Message>WM_PROTOCOLS: quit()\n \
<Key>p: previous() \n \
<Key>P: previous() \n \
<Key>n: next() \n \
<Key>N: next() \n \
<Configure>: notifyresize()";
/*
* X data structures
*/
Colormap xColormap;
Display * xDisplay;
Pixmap xImagePixmap;
Visual * xVisual;
XImage * xImage;
GC xWinGc;
int xImageDepth, xScreen, xRedMask, xGreenMask, xBlueMask,
xOffset = 0, yOffset = 0, grabX = -1, grabY = -1;
unsigned char basePixel = 0;
/*
* TIFF data structures
*/
TIFF * tfFile = NULL;
uint32 tfImageWidth, tfImageHeight;
uint16 tfBitsPerSample, tfSamplesPerPixel, tfPlanarConfiguration,
tfPhotometricInterpretation, tfGrayResponseUnit,
tfImageDepth, tfBytesPerRow;
int tfDirectory = 0, tfMultiPage = False;
double tfUnitMap, tfGrayResponseUnitMap[] = {
-1, -10, -100, -1000, -10000, -100000
};
/*
* display data structures
*/
double *dRed, *dGreen, *dBlue;
/*
* shared data structures
*/
uint16 * redMap = NULL, *greenMap = NULL, *blueMap = NULL,
*grayMap = NULL, colormapSize;
char * imageMemory;
char * fileName;
int
main(int argc, char **argv)
{
XSetWindowAttributes window_attributes;
Widget widget_list[3];
Arg args[5];
setbuf(stdout, NULL); setbuf(stderr, NULL);
shellWidget = XtInitialize(argv[0], "XTiff", shellOptions,
XtNumber(shellOptions), &argc, argv);
XSetErrorHandler(XTiffErrorHandler);
XtGetApplicationResources(shellWidget, &appData,
(XtResourceList) clientResources, (Cardinal) XtNumber(clientResources),
(ArgList) NULL, (Cardinal) 0);
if ((argc <= 1) || (argc > 2) || appData.help)
Usage();
if (appData.verbose == False) {
TIFFSetErrorHandler(0);
TIFFSetWarningHandler(0);
}
fileName = argv[1];
xDisplay = XtDisplay(shellWidget);
xScreen = DefaultScreen(xDisplay);
OpenTIFFFile();
GetTIFFHeader();
SimpleGammaCorrection();
GetVisual();
GetTIFFImage();
/*
* Send visual, colormap, depth and iconPixmap to shellWidget.
* Sending the visual to the shell is only possible with the advent of R4.
*/
XtSetArg(args[0], XtNvisual, xVisual);
XtSetArg(args[1], XtNcolormap, xColormap);
XtSetArg(args[2], XtNdepth,
xImageDepth == 1 ? DefaultDepth(xDisplay, xScreen) : xImageDepth);
XtSetArg(args[3], XtNiconPixmap,
XCreateBitmapFromData(xDisplay, RootWindow(xDisplay, xScreen),
xtifficon_bits, xtifficon_width, xtifficon_height));
XtSetArg(args[4], XtNallowShellResize, True);
XtSetValues(shellWidget, args, 5);
/*
* widget instance hierarchy
*/
formWidget = XtCreateManagedWidget("form", formWidgetClass,
shellWidget, formArgs, XtNumber(formArgs));
widget_list[0] = listWidget = XtCreateWidget("list",
listWidgetClass, formWidget, listArgs, XtNumber(listArgs));
widget_list[1] = labelWidget = XtCreateWidget("label",
labelWidgetClass, formWidget, labelArgs, XtNumber(labelArgs));
widget_list[2] = imageWidget = XtCreateWidget("image",
widgetClass, formWidget, imageArgs, XtNumber(imageArgs));
XtManageChildren(widget_list, XtNumber(widget_list));
/*
* initial widget sizes - for small images let xtiff size itself
*/
if (tfImageWidth >= appData.viewportWidth) {
XtSetArg(args[0], XtNwidth, appData.viewportWidth);
XtSetValues(shellWidget, args, 1);
}
if (tfImageHeight >= appData.viewportHeight) {
XtSetArg(args[0], XtNheight, appData.viewportHeight);
XtSetValues(shellWidget, args, 1);
}
XtSetArg(args[0], XtNwidth, tfImageWidth);
XtSetArg(args[1], XtNheight, tfImageHeight);
XtSetValues(imageWidget, args, 2);
/*
* formWidget uses these constraints but they are stored in the children.
*/
XtSetArg(args[0], XtNfromVert, listWidget);
XtSetValues(imageWidget, args, 1);
XtSetArg(args[0], XtNfromHoriz, listWidget);
XtSetValues(labelWidget, args, 1);
SetNameLabel();
XtAddCallback(listWidget, XtNcallback, (XtCallbackProc) SelectProc,
(XtPointer) NULL);
XtAddActions(actionsTable, XtNumber(actionsTable));
XtSetArg(args[0], XtNtranslations,
XtParseTranslationTable(translationsTable));
XtSetValues(formWidget, &args[0], 1);
XtSetValues(imageWidget, &args[0], 1);
/*
* This is intended to be a little faster than going through
* the translation manager.
*/
XtAddEventHandler(imageWidget, ExposureMask | ButtonPressMask
| ButtonReleaseMask | Button1MotionMask | KeyPressMask,
False, EventProc, NULL);
XtRealizeWidget(shellWidget);
window_attributes.cursor = XCreateFontCursor(xDisplay, XC_fleur);
XChangeWindowAttributes(xDisplay, XtWindow(imageWidget),
CWCursor, &window_attributes);
CreateXImage();
XtMainLoop();
return 0;
}
void
OpenTIFFFile()
{
if (tfFile != NULL)
TIFFClose(tfFile);
if ((tfFile = TIFFOpen(fileName, "r")) == NULL) {
fprintf(appData.verbose ? stderr : stdout,
"xtiff: can't open %s as a TIFF file\n", fileName);
exit(0);
}
tfMultiPage = (TIFFLastDirectory(tfFile) ? False : True);
}
void
GetTIFFHeader()
{
register int i;
if (!TIFFSetDirectory(tfFile, tfDirectory)) {
fprintf(stderr, "xtiff: can't seek to directory %d in %s\n",
tfDirectory, fileName);
exit(0);
}
TIFFGetField(tfFile, TIFFTAG_IMAGEWIDTH, &tfImageWidth);
TIFFGetField(tfFile, TIFFTAG_IMAGELENGTH, &tfImageHeight);
/*
* If the following tags aren't present then use the TIFF defaults.
*/
TIFFGetFieldDefaulted(tfFile, TIFFTAG_BITSPERSAMPLE, &tfBitsPerSample);
TIFFGetFieldDefaulted(tfFile, TIFFTAG_SAMPLESPERPIXEL, &tfSamplesPerPixel);
TIFFGetFieldDefaulted(tfFile, TIFFTAG_PLANARCONFIG, &tfPlanarConfiguration);
TIFFGetFieldDefaulted(tfFile, TIFFTAG_GRAYRESPONSEUNIT, &tfGrayResponseUnit);
tfUnitMap = tfGrayResponseUnitMap[tfGrayResponseUnit];
colormapSize = 1 << tfBitsPerSample;
tfImageDepth = tfBitsPerSample * tfSamplesPerPixel;
dRed = (double *) malloc(colormapSize * sizeof(double));
dGreen = (double *) malloc(colormapSize * sizeof(double));
dBlue = (double *) malloc(colormapSize * sizeof(double));
MCHECK(dRed); MCHECK(dGreen); MCHECK(dBlue);
/*
* If TIFFTAG_PHOTOMETRIC is not present then assign a reasonable default.
* The TIFF 5.0 specification doesn't give a default.
*/
if (!TIFFGetField(tfFile, TIFFTAG_PHOTOMETRIC,
&tfPhotometricInterpretation)) {
if (tfSamplesPerPixel != 1)
tfPhotometricInterpretation = PHOTOMETRIC_RGB;
else if (tfBitsPerSample == 1)
tfPhotometricInterpretation = PHOTOMETRIC_MINISBLACK;
else if (TIFFGetField(tfFile, TIFFTAG_COLORMAP,
&redMap, &greenMap, &blueMap)) {
tfPhotometricInterpretation = PHOTOMETRIC_PALETTE;
redMap = greenMap = blueMap = NULL;
} else
tfPhotometricInterpretation = PHOTOMETRIC_MINISBLACK;
}
/*
* Given TIFFTAG_PHOTOMETRIC extract or create the response curves.
*/
switch (tfPhotometricInterpretation) {
case PHOTOMETRIC_RGB:
redMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
greenMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
blueMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
MCHECK(redMap); MCHECK(greenMap); MCHECK(blueMap);
for (i = 0; i < colormapSize; i++)
dRed[i] = dGreen[i] = dBlue[i]
= (double) SCALE(i, colormapSize - 1);
break;
case PHOTOMETRIC_PALETTE:
if (!TIFFGetField(tfFile, TIFFTAG_COLORMAP,
&redMap, &greenMap, &blueMap)) {
redMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
greenMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
blueMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
MCHECK(redMap); MCHECK(greenMap); MCHECK(blueMap);
for (i = 0; i < colormapSize; i++)
dRed[i] = dGreen[i] = dBlue[i]
= (double) SCALE(i, colormapSize - 1);
} else {
CheckAndCorrectColormap();
for (i = 0; i < colormapSize; i++) {
dRed[i] = (double) redMap[i];
dGreen[i] = (double) greenMap[i];
dBlue[i] = (double) blueMap[i];
}
}
break;
case PHOTOMETRIC_MINISWHITE:
redMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
greenMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
blueMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
MCHECK(redMap); MCHECK(greenMap); MCHECK(blueMap);
for (i = 0; i < colormapSize; i++)
dRed[i] = dGreen[i] = dBlue[i] = (double)
SCALE(colormapSize-1-i, colormapSize-1);
break;
case PHOTOMETRIC_MINISBLACK:
redMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
greenMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
blueMap = (uint16 *) malloc(colormapSize * sizeof(uint16));
MCHECK(redMap); MCHECK(greenMap); MCHECK(blueMap);
for (i = 0; i < colormapSize; i++)
dRed[i] = dGreen[i] = dBlue[i] = (double) SCALE(i, colormapSize-1);
break;
default:
fprintf(stderr,
"xtiff: can't display photometric interpretation type %d\n",
tfPhotometricInterpretation);
exit(0);
}
}
void
SetNameLabel()
{
char buffer[BUFSIZ];
Arg args[1];
if (tfMultiPage)
snprintf(buffer, sizeof(buffer), "%s - page %d", fileName, tfDirectory);
else
snprintf(buffer, sizeof(buffer), "%s", fileName);
XtSetArg(args[0], XtNlabel, buffer);
XtSetValues(labelWidget, args, 1);
}
/*
* Many programs get TIFF colormaps wrong. They use 8-bit colormaps instead of
* 16-bit colormaps. This function is a heuristic to detect and correct this.
*/
void
CheckAndCorrectColormap()
{
register int i;
for (i = 0; i < colormapSize; i++)
if ((redMap[i] > 255) || (greenMap[i] > 255) || (blueMap[i] > 255))
return;
for (i = 0; i < colormapSize; i++) {
redMap[i] = SCALE(redMap[i], 255);
greenMap[i] = SCALE(greenMap[i], 255);
blueMap[i] = SCALE(blueMap[i], 255);
}
TIFFWarning(fileName, "Assuming 8-bit colormap");
}
void
SimpleGammaCorrection()
{
register int i;
register double i_gamma = 1.0 / appData.gamma;
for (i = 0; i < colormapSize; i++) {
if (((tfPhotometricInterpretation == PHOTOMETRIC_MINISWHITE)
&& (i == colormapSize - 1))
|| ((tfPhotometricInterpretation == PHOTOMETRIC_MINISBLACK)
&& (i == 0)))
redMap[i] = greenMap[i] = blueMap[i] = 0;
else {
redMap[i] = ROUND((pow(dRed[i] / 65535.0, i_gamma) * 65535.0));
greenMap[i] = ROUND((pow(dGreen[i] / 65535.0, i_gamma) * 65535.0));
blueMap[i] = ROUND((pow(dBlue[i] / 65535.0, i_gamma) * 65535.0));
}
}
free(dRed); free(dGreen); free(dBlue);
}
static char* classNames[] = {
"StaticGray",
"GrayScale",
"StaticColor",
"PseudoColor",
"TrueColor",
"DirectColor"
};
/*
* Current limitation: the visual is set initially by the first file.
* It cannot be changed.
*/
void
GetVisual()
{
XColor *colors = NULL;
unsigned long *pixels = NULL;
unsigned long i;
switch (tfImageDepth) {
/*
* X really wants a 32-bit image with the fourth channel unused,
* but the visual structure thinks it's 24-bit. bitmap_unit is 32.
*/
case 32:
case 24:
if (SearchVisualList(24, DirectColor, &xVisual) == False) {
fprintf(stderr, "xtiff: 24-bit DirectColor visual not available\n");
exit(0);
}
colors = (XColor *) malloc(3 * colormapSize * sizeof(XColor));
MCHECK(colors);
for (i = 0; i < colormapSize; i++) {
colors[i].pixel = (i << 16) + (i << 8) + i;
colors[i].red = redMap[i];
colors[i].green = greenMap[i];
colors[i].blue = blueMap[i];
colors[i].flags = DoRed | DoGreen | DoBlue;
}
xColormap = XCreateColormap(xDisplay, RootWindow(xDisplay, xScreen),
xVisual, AllocAll);
XStoreColors(xDisplay, xColormap, colors, colormapSize);
break;
case 8:
case 4:
case 2:
/*
* We assume that systems with 24-bit visuals also have 8-bit visuals.
* We don't promote from 8-bit PseudoColor to 24/32 bit DirectColor.
*/
switch (tfPhotometricInterpretation) {
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
if (SearchVisualList((int) tfImageDepth, GrayScale, &xVisual) == True)
break;
case PHOTOMETRIC_PALETTE:
if (SearchVisualList((int) tfImageDepth, PseudoColor, &xVisual) == True)
break;
default:
fprintf(stderr, "xtiff: Unsupported TIFF/X configuration\n");
exit(0);
}
colors = (XColor *) malloc(colormapSize * sizeof(XColor));
MCHECK(colors);
for (i = 0; i < colormapSize; i++) {
colors[i].pixel = i;
colors[i].red = redMap[i];
colors[i].green = greenMap[i];
colors[i].blue = blueMap[i];
colors[i].flags = DoRed | DoGreen | DoBlue;
}
/*
* xtiff's colormap allocation is private. It does not attempt
* to detect whether any existing colormap entries are suitable
* for its use. This will cause colormap flashing. Furthermore,
* background and foreground are taken from the environment.
* For example, the foreground color may be red when the visual
* is GrayScale. If the colormap is completely populated,
* Xt will not be able to allocate fg and bg.
*/
if (tfImageDepth == 8)
xColormap = XCreateColormap(xDisplay, RootWindow(xDisplay, xScreen),
xVisual, AllocAll);
else {
xColormap = XCreateColormap(xDisplay, RootWindow(xDisplay, xScreen),
xVisual, AllocNone);
pixels = (unsigned long *)
malloc(colormapSize * sizeof(unsigned long));
MCHECK(pixels);
(void) XAllocColorCells(xDisplay, xColormap, True,
NULL, 0, pixels, colormapSize);
basePixel = (unsigned char) pixels[0];
free(pixels);
}
XStoreColors(xDisplay, xColormap, colors, colormapSize);
break;
case 1:
xImageDepth = 1;
xVisual = DefaultVisual(xDisplay, xScreen);
xColormap = DefaultColormap(xDisplay, xScreen);
break;
default:
fprintf(stderr, "xtiff: unsupported image depth %d\n", tfImageDepth);
exit(0);
}
if (appData.verbose == True)
fprintf(stderr, "%s: Using %d-bit %s visual.\n",
fileName, xImageDepth, classNames[xVisual->class]);
if (colors != NULL)
free(colors);
if (grayMap != NULL)
free(grayMap);
if (redMap != NULL)
free(redMap);
if (greenMap != NULL)
free(greenMap);
if (blueMap != NULL)
free(blueMap);
colors = NULL; grayMap = redMap = greenMap = blueMap = NULL;
}
/*
* Search for an appropriate visual. Promote where necessary.
* Check to make sure that ENOUGH colormap entries are writeable.
* basePixel was determined when XAllocColorCells() contiguously
* allocated enough entries. basePixel is used below in GetTIFFImage.
*/
Boolean
SearchVisualList(image_depth, visual_class, visual)
int image_depth, visual_class;
Visual **visual;
{
XVisualInfo template_visual, *visual_list, *vl;
int i, n_visuals;
template_visual.screen = xScreen;
vl = visual_list = XGetVisualInfo(xDisplay, VisualScreenMask,
&template_visual, &n_visuals);
if (n_visuals == 0) {
fprintf(stderr, "xtiff: visual list not available\n");
exit(0);
}
for (i = 0; i < n_visuals; vl++, i++) {
if ((vl->class == visual_class) && (vl->depth >= image_depth)
&& (vl->visual->map_entries >= (1 << vl->depth))) {
*visual = vl->visual;
xImageDepth = vl->depth;
xRedMask = vl->red_mask;
xGreenMask = vl->green_mask;
xBlueMask = vl->blue_mask;
XFree((char *) visual_list);
return True;
}
}
XFree((char *) visual_list);
return False;
}
void
GetTIFFImage()
{
int pixel_map[3], red_shift, green_shift, blue_shift;
char *scan_line, *output_p, *input_p;
uint32 i, j;
uint16 s;
scan_line = (char *) malloc(tfBytesPerRow = TIFFScanlineSize(tfFile));
MCHECK(scan_line);
if ((tfImageDepth == 32) || (tfImageDepth == 24)) {
output_p = imageMemory = (char *)
malloc(tfImageWidth * tfImageHeight * 4);
MCHECK(imageMemory);
/*
* Handle different color masks for different frame buffers.
*/
if (ImageByteOrder(xDisplay) == LSBFirst) { /* DECstation 5000 */
red_shift = pixel_map[0] = xRedMask == 0xFF000000 ? 3
: (xRedMask == 0xFF0000 ? 2 : (xRedMask == 0xFF00 ? 1 : 0));
green_shift = pixel_map[1] = xGreenMask == 0xFF000000 ? 3
: (xGreenMask == 0xFF0000 ? 2 : (xGreenMask == 0xFF00 ? 1 : 0));
blue_shift = pixel_map[2] = xBlueMask == 0xFF000000 ? 3
: (xBlueMask == 0xFF0000 ? 2 : (xBlueMask == 0xFF00 ? 1 : 0));
} else { /* Ardent */
red_shift = pixel_map[0] = xRedMask == 0xFF000000 ? 0
: (xRedMask == 0xFF0000 ? 1 : (xRedMask == 0xFF00 ? 2 : 3));
green_shift = pixel_map[0] = xGreenMask == 0xFF000000 ? 0
: (xGreenMask == 0xFF0000 ? 1 : (xGreenMask == 0xFF00 ? 2 : 3));
blue_shift = pixel_map[0] = xBlueMask == 0xFF000000 ? 0
: (xBlueMask == 0xFF0000 ? 1 : (xBlueMask == 0xFF00 ? 2 : 3));
}
if (tfPlanarConfiguration == PLANARCONFIG_CONTIG) {
for (i = 0; i < tfImageHeight; i++) {
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0)
break;
for (input_p = scan_line, j = 0; j < tfImageWidth; j++) {
*(output_p + red_shift) = *input_p++;
*(output_p + green_shift) = *input_p++;
*(output_p + blue_shift) = *input_p++;
output_p += 4;
if (tfSamplesPerPixel == 4) /* skip the fourth channel */
input_p++;
}
}
} else {
for (s = 0; s < tfSamplesPerPixel; s++) {
if (s == 3) /* skip the fourth channel */
continue;
for (i = 0; i < tfImageHeight; i++) {
if (TIFFReadScanline(tfFile, scan_line, i, s) < 0)
break;
input_p = scan_line;
output_p = imageMemory + (i*tfImageWidth*4) + pixel_map[s];
for (j = 0; j < tfImageWidth; j++, output_p += 4)
*output_p = *input_p++;
}
}
}
} else {
if (xImageDepth == tfImageDepth) {
output_p = imageMemory = (char *)
malloc(tfBytesPerRow * tfImageHeight);
MCHECK(imageMemory);
for (i = 0; i < tfImageHeight; i++, output_p += tfBytesPerRow)
if (TIFFReadScanline(tfFile, output_p, i, 0) < 0)
break;
} else if ((xImageDepth == 8) && (tfImageDepth == 4)) {
output_p = imageMemory = (char *)
malloc(tfBytesPerRow * 2 * tfImageHeight + 2);
MCHECK(imageMemory);
/*
* If a scanline is of odd size the inner loop below will overshoot.
* This is handled very simply by recalculating the start point at
* each scanline and padding imageMemory a little at the end.
*/
for (i = 0; i < tfImageHeight; i++) {
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0)
break;
output_p = &imageMemory[i * tfImageWidth];
input_p = scan_line;
for (j = 0; j < tfImageWidth; j += 2, input_p++) {
*output_p++ = (*input_p >> 4) + basePixel;
*output_p++ = (*input_p & 0xf) + basePixel;
}
}
} else if ((xImageDepth == 8) && (tfImageDepth == 2)) {
output_p = imageMemory = (char *)
malloc(tfBytesPerRow * 4 * tfImageHeight + 4);
MCHECK(imageMemory);
for (i = 0; i < tfImageHeight; i++) {
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0)
break;
output_p = &imageMemory[i * tfImageWidth];
input_p = scan_line;
for (j = 0; j < tfImageWidth; j += 4, input_p++) {
*output_p++ = (*input_p >> 6) + basePixel;
*output_p++ = ((*input_p >> 4) & 3) + basePixel;
*output_p++ = ((*input_p >> 2) & 3) + basePixel;
*output_p++ = (*input_p & 3) + basePixel;
}
}
} else if ((xImageDepth == 4) && (tfImageDepth == 2)) {
output_p = imageMemory = (char *)
malloc(tfBytesPerRow * 2 * tfImageHeight + 2);
MCHECK(imageMemory);
for (i = 0; i < tfImageHeight; i++) {
if (TIFFReadScanline(tfFile, scan_line, i, 0) < 0)
break;
output_p = &imageMemory[i * tfBytesPerRow * 2];
input_p = scan_line;
for (j = 0; j < tfImageWidth; j += 4, input_p++) {
*output_p++ = (((*input_p>>6) << 4)
| ((*input_p >> 4) & 3)) + basePixel;
*output_p++ = ((((*input_p>>2) & 3) << 4)
| (*input_p & 3)) + basePixel;
}
}
} else {
fprintf(stderr,
"xtiff: can't handle %d-bit TIFF file on an %d-bit display\n",
tfImageDepth, xImageDepth);
exit(0);
}
}
free(scan_line);
}
void
CreateXImage()
{
XGCValues gc_values;
GC bitmap_gc;
xOffset = yOffset = 0;
grabX = grabY = -1;
xImage = XCreateImage(xDisplay, xVisual, xImageDepth,
xImageDepth == 1 ? XYBitmap : ZPixmap, /* offset */ 0,
(char *) imageMemory, tfImageWidth, tfImageHeight,
/* bitmap_pad */ 8, /* bytes_per_line */ 0);
/*
* libtiff converts LSB data into MSB but doesn't change the FillOrder tag.
*/
if (xImageDepth == 1)
xImage->bitmap_bit_order = MSBFirst;
if (xImageDepth <= 8)
xImage->byte_order = MSBFirst;
/*
* create an appropriate GC
*/
gc_values.function = GXcopy;
gc_values.plane_mask = AllPlanes;
if (tfPhotometricInterpretation == PHOTOMETRIC_MINISBLACK) {
gc_values.foreground = XWhitePixel(xDisplay, xScreen);
gc_values.background = XBlackPixel(xDisplay, xScreen);
} else {
gc_values.foreground = XBlackPixel(xDisplay, xScreen);
gc_values.background = XWhitePixel(xDisplay, xScreen);
}
xWinGc = XCreateGC(xDisplay, XtWindow(shellWidget),
GCFunction | GCPlaneMask | GCForeground | GCBackground, &gc_values);
/*
* create the pixmap and load the image
*/
if (appData.usePixmap == True) {
xImagePixmap = XCreatePixmap(xDisplay, RootWindow(xDisplay, xScreen),
xImage->width, xImage->height, xImageDepth);
/*
* According to the O'Reilly X Protocol Reference Manual, page 53,
* "A pixmap depth of one is always supported and listed, but windows
* of depth one might not be supported." Therefore we create a pixmap
* of depth one and use XCopyPlane(). This is idiomatic.
*/
if (xImageDepth == 1) { /* just pass the bits through */
gc_values.foreground = 1; /* foreground describes set bits */
gc_values.background = 0; /* background describes clear bits */
bitmap_gc = XCreateGC(xDisplay, xImagePixmap,
GCForeground | GCBackground, &gc_values);
XPutImage(xDisplay, xImagePixmap, bitmap_gc, xImage,
0, 0, 0, 0, xImage->width, xImage->height);
} else
XPutImage(xDisplay, xImagePixmap, xWinGc, xImage,
0, 0, 0, 0, xImage->width, xImage->height);
XDestroyImage(xImage);
free(imageMemory);
}
}
XtCallbackProc
SelectProc(w, unused_1, unused_2)
Widget w;
caddr_t unused_1;
caddr_t unused_2;
{
XawListReturnStruct *list_return;
list_return = XawListShowCurrent(w);
switch (list_return->list_index) {
case ButtonQuit:
QuitProc();
break;
case ButtonPreviousPage:
PreviousProc();
break;
case ButtonNextPage:
NextProc();
break;
default:
fprintf(stderr, "error in SelectProc\n");
exit(0);
}
XawListUnhighlight(w);
}
void
QuitProc(void)
{
exit(0);
}
void
NextProc()
{
PageProc(ButtonNextPage);
}
void
PreviousProc()
{
PageProc(ButtonPreviousPage);
}
void
PageProc(direction)
int direction;
{
XEvent fake_event;
Arg args[4];
switch (direction) {
case ButtonPreviousPage:
if (tfDirectory > 0)
TIFFSetDirectory(tfFile, --tfDirectory);
else
return;
break;
case ButtonNextPage:
if (TIFFReadDirectory(tfFile) == True)
tfDirectory++;
else
return;