File: sgigt.c

package info (click to toggle)
libtiff3 3.4beta037-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 2,732 kB
  • ctags: 3,274
  • sloc: ansic: 32,425; sh: 2,610; makefile: 1,515
file content (984 lines) | stat: -rw-r--r-- 23,786 bytes parent folder | download | duplicates (2)
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
/* $Header: /d1/sam/tiff/tools/RCS/sgigt.c,v 1.70 1997/08/31 23:54:02 sam Exp $ */

/*
 * Copyright (c) 1988-1997 Sam Leffler
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <gl.h>
#include <device.h>

#include "tiffio.h"

#ifndef TRUE
#define	TRUE	1
#define	FALSE	0
#endif

/* XXX fudge adjustment for window borders */
#define	YFUDGE	20
#define	XFUDGE	20

static	tileContigRoutine putContig;
static	tileSeparateRoutine putSeparate;
static	uint32 width, height;		/* window width & height */
static	uint32* raster = NULL;		/* displayable image */

extern	Colorindex greyi(int);
static	void setupColormapSupport(TIFFRGBAImage*);
static	void putContigAndDraw(TIFFRGBAImage*, uint32*,
    uint32, uint32, uint32, uint32, int32, int32, unsigned char*);
static	void putSeparateAndDraw(TIFFRGBAImage*, uint32*,
    uint32, uint32, uint32, uint32, int32, int32,
    unsigned char*, unsigned char*, unsigned char*, unsigned char*);

static	int prevImage(char* argv[], int ix, int b, int e, int wrap);
static	int nextImage(char* argv[], int ix, int b, int e, int wrap);
static	void usage(void);
static	uint16 photoArg(const char*);
static	void beep(void);

extern	char* optarg;
extern	int optind;

int
main(int argc, char* argv[])
{
    static Cursor hourglass = {
	0x1ff0, 0x1ff0, 0x0820, 0x0820,
	0x0820, 0x0c60, 0x06c0, 0x0100,
	0x0100, 0x06c0, 0x0c60, 0x0820,
	0x0820, 0x0820, 0x1ff0, 0x1ff0
    };
    int isRGB0 = -1, isRGB;
    int verbose = 0;
    int stoponerr = 0;			/* stop on read error */
    char* filename;
    TIFF* tif = NULL;
    int fg = 0;
    int c;
    int dirnum = -1;
    int order0 = 0, order;
    uint32 diroff = 0;
    uint16 photo0 = (uint16) -1, photo;
    long x, y, xmax, ymax;
    int ix, nix;
    TIFFErrorHandler oerror = TIFFSetErrorHandler(NULL);
    TIFFErrorHandler owarning = TIFFSetWarningHandler(NULL);
    uint32 w, h;
    long wid = -1;

    while ((c = getopt(argc, argv, "d:o:p:cerflmsvw")) != -1)
	switch (c) {
	case 'c':
	    isRGB0 = 0;
	    break;
	case 'd':
	    dirnum = atoi(optarg);
	    break;
	case 'e':
	    oerror = TIFFSetErrorHandler(oerror);
	    break;
	case 'f':
	    fg = 1;
	    break;
	case 'l':
	    order0 = FILLORDER_LSB2MSB;
	    break;
	case 'm':
	    order0 = FILLORDER_MSB2LSB;
	    break;
	case 'o':
	    diroff = strtoul(optarg, NULL, 0);
	    break;
	case 'p':
	    photo0 = photoArg(optarg);
	    break;
	case 'r':
	    isRGB0 = 1;
	    break;
	case 's':
	    stoponerr = 1;
	    break;
	case 'w':
	    owarning = TIFFSetWarningHandler(owarning);
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case '?':
	    usage();
	    /*NOTREACHED*/
	}
    if (argc - optind < 1)
	usage();
    xmax = getgdesc(GD_XPMAX) - XFUDGE;
    ymax = getgdesc(GD_YPMAX) - YFUDGE;
    ix = optind;
    do {
	tif = TIFFOpen(argv[ix], "r");
    } while (tif == NULL && (ix = nextImage(argv, ix, optind, argc, FALSE)));
    if (tif == NULL)
	exit(0);
    if (ix == optind) {
	/*
	 * Set initial directory if user-specified
	 * file was opened successfully.
	 */
	if (dirnum != -1 && !TIFFSetDirectory(tif, dirnum))
	    TIFFError(argv[ix], "Error, seeking to directory %d", dirnum);
	if (diroff != 0 && !TIFFSetSubDirectory(tif, diroff))
	    TIFFError(argv[ix], "Error, setting subdirectory at %#x", diroff);
    }
    isRGB = isRGB0;
    order = order0;
    photo = photo0;
    goto newfile0;
    for (;;) {
	TIFFRGBAImage img;
	char title[1024];			/* window title line */
	const char* cp;
	int isrgb;

	if (order)
	    TIFFSetField(tif, TIFFTAG_FILLORDER, order);
	if (photo != (uint16) -1)
	    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, photo);
	if (!TIFFRGBAImageBegin(&img, tif, stoponerr, title)) {
	    TIFFError(filename, title);
	    goto bad2;
	}
	/*
	 * Use a full-color window if the image is
	 * full color or a palette image and the
	 * hardware support is present.
	 */
	isrgb = isRGB;
	if (isrgb == -1)
	    isrgb = (img.bitspersample >= 8 &&
		(img.photometric == PHOTOMETRIC_RGB ||
		 img.photometric == PHOTOMETRIC_YCBCR ||
		 img.photometric == PHOTOMETRIC_SEPARATED ||
		 img.photometric == PHOTOMETRIC_PALETTE ||
		 img.photometric == PHOTOMETRIC_LOGLUV));
	/*
	 * Check to see if the hardware can display 24-bit RGB.
	 */
	if (isrgb && getgdesc(GD_BITS_NORM_SNG_RED) < img.bitspersample &&
	  !getgdesc(GD_DITHER)) {
	    if (verbose)
		printf("Warning, display is incapable of full RGB,%s\n",
			 " using dithered colormap");
	    isrgb = 0;
	}
	/*
	 * Colormap-based display is done by overriding the put
	 * routine to install a private method that understands
	 * how to convert RGBA values to suitable colormap indices.
	 */
	if (!isrgb)
	    setupColormapSupport(&img);
	/*
	 * Override default ``put routine'' with private
	 * routine that also draws the raster on the display.
	 */
	if (img.put.any == 0) {
	    TIFFError(filename,
		"No \"put\" routine; must not handle image format");
	    goto bad3;
	}
	if (img.isContig) {
	    putContig = img.put.contig;
	    img.put.contig = putContigAndDraw;
	} else {
	    putSeparate = img.put.separate;
	    img.put.separate = putSeparateAndDraw;
	}
	/*
	 * Setup the image raster as required.
	 */
	if ((w = img.width) > xmax)
	    w = xmax;
	if ((h = img.height) > ymax)
	    h = ymax;
	if (w != width || h != height) {
	    if (raster != NULL)
		_TIFFfree(raster), raster = NULL;
	    raster = (uint32*) _TIFFmalloc(w * h * sizeof (uint32));
	    if (raster == NULL) {
		width = height = 0;
		TIFFError(filename, "No space for raster buffer");
		goto bad3;
	    }
	    width = w;
	    height = h;
	}
	/*
	 * Create a new window or reconfigure an existing
	 * one to suit the image to be displayed.
	 */
	if (wid < 0) {
	    x = (xmax+XFUDGE-width)/2;
	    y = (ymax+YFUDGE-height)/2;
	    prefposition(x, x+width-1, y, y+height-1);
	    cp = strrchr(filename, '/');
	    sprintf(title, "%s [%u] %s",
		cp == NULL ? filename : cp+1,
		(unsigned int) TIFFCurrentDirectory(tif),
		isrgb ? " rgb" : " cmap");
	    if (fg)
		foreground();
	    wid = winopen(title);
	    if (wid < 0) {
		TIFFError(filename, "Can not create window");
		TIFFRGBAImageEnd(&img);
		break;
	    }
	    curstype(C16X1);
	    defcursor(1, hourglass);
	    qdevice(LEFTMOUSE);
	    qdevice(MIDDLEMOUSE);
	    qdevice(RIGHTMOUSE);
	    qdevice(KEYBD);
	    qdevice(PAGEUPKEY);
	    qdevice(PAGEDOWNKEY);
	    qdevice(HOMEKEY);
	    qdevice(ENDKEY);
	} else {
	    x = (xmax+XFUDGE-width)/2;
	    y = (ymax+YFUDGE-height)/2;
	    winposition(x, x+width-1, y, y+height-1);
	    viewport(0, width-1, 0, height-1);
	    cp = strrchr(filename, '/');
	    sprintf(title, "%s [%u] %s",
		cp == NULL ? filename : cp+1,
		(unsigned int) TIFFCurrentDirectory(tif),
		isrgb ? " rgb" : " cmap");
	    wintitle(title);
	}
	singlebuffer();
	if (isrgb) {
	    RGBmode();
	    gconfig();
	} else {
	    cmode();
	    gconfig();
	}
	/*
	 * Fetch the image.
	 */
	setcursor(1, 0, 0);
	greyi(225);
	clear();
	(void) TIFFRGBAImageGet(&img, raster, width, height);
	setcursor(0, 0, 0);
	/*
	 * Process input.
	 */
	for (;;) {
	    short val;
	    switch (qread(&val)) {
	    case KEYBD:
		switch (val) {
		case 'b':			/* photometric MinIsBlack */
		    photo = PHOTOMETRIC_MINISBLACK;
		    goto newpage;
		case 'l':			/* lsb-to-msb FillOrder */
		    order = FILLORDER_LSB2MSB;
		    goto newpage;
		case 'm':			/* msb-to-lsb FillOrder */
		    order = FILLORDER_MSB2LSB;
		    goto newpage;
		case 'c':			/* colormap visual */
		    isRGB = 0;
		    goto newpage;
		case 'r':			/* RGB visual */
		    isRGB = 1;
		    goto newpage;
		case 'w':			/* photometric MinIsWhite */
		    photo = PHOTOMETRIC_MINISWHITE;
		    goto newpage;
		case 'W':			/* toggle warnings */
		    owarning = TIFFSetWarningHandler(owarning);
		    goto newpage;
		case 'E':			/* toggle errors */
		    oerror = TIFFSetErrorHandler(oerror);
		    goto newpage;
		case 'z':			/* reset to defaults */
		case 'Z':
		    order = order0;
		    photo = photo0;
		    isRGB = isRGB0;
		    if (owarning == NULL)
			owarning = TIFFSetWarningHandler(NULL);
		    if (oerror == NULL)
			oerror = TIFFSetErrorHandler(NULL);
		    goto newpage;
		case 'q':			/* exit */
		case '\033':
		    TIFFRGBAImageEnd(&img);
		    goto done;
		}
		break;
	    case PAGEUPKEY:			/* previous logical image */
		if (val) {
		    if (TIFFCurrentDirectory(tif) > 0) {
			if (TIFFSetDirectory(tif, TIFFCurrentDirectory(tif)-1))
			    goto newpage;
			beep();		/* XXX */
		    } else {
			ix = prevImage(argv, ix, optind, argc, TRUE);
			/* XXX set directory to last image in new file */
			goto newfile;
		    }
		}
		break;
	    case PAGEDOWNKEY:			/* next logical image */
		if (val) {
		    if (!TIFFLastDirectory(tif)) {
			if (TIFFReadDirectory(tif))
			    goto newpage;
			beep();		/* XXX */
		    } else {
			ix = nextImage(argv, ix, optind, argc, TRUE);
			goto newfile;
		    }
		}
		break;
	    case HOMEKEY:			/* 1st image in current file */
		if (val) {
		    if (TIFFSetDirectory(tif, 0))
			goto newpage;
		    beep();
		}
		break;
	    case ENDKEY:			/* last image in current file */
		if (val) {
		    /* XXX */
		    beep();
		}
		break;
	    case RIGHTMOUSE:			/* previous file */
		if (val) {
		    if (nix = prevImage(argv, ix, optind, argc, FALSE)) {
			ix = nix;
			goto newfile;
		    }
		    beep();
		}
		break;
	    case LEFTMOUSE:			/* next file */
		if (val) {
		    if (nix = nextImage(argv, ix, optind, argc, FALSE)) {
			ix = nix;
			goto newfile;
		    }
		    beep();
		}
		break;
	    case MIDDLEMOUSE:			/* first file */
		if (val) {
		    if (nix = nextImage(argv, optind-1, optind, argc, FALSE)) {
			ix = nix;
			goto newfile;
		    }
		    beep();
		}
		break;
	    case REDRAW:
		lrectwrite(0, 0, width-1, height-1, raster);
		break;
	    }
	}
    newfile:
	TIFFRGBAImageEnd(&img);
	if (tif != NULL && argv[ix] != filename)
	    TIFFClose(tif), tif = NULL;
	/* fall thru... */
    newfile0:
	if (argv[ix] == NULL)
	    break;
	filename = argv[ix];
	if (tif == NULL) {
	    tif = TIFFOpen(filename, "r");
	    if (tif == NULL)
		goto bad1;
	    isRGB = isRGB0;
	    order = order0;
	    photo = photo0;
	}
	continue;
    newpage:
	TIFFRGBAImageEnd(&img);
	continue;
    bad3:
	TIFFRGBAImageEnd(&img);
    bad2:
	TIFFClose(tif), tif = NULL;
    bad1:
	argv[ix] = NULL;			/* don't revisit file */
	ix = nextImage(argv, ix, optind, argc, TRUE);
	goto newfile0;
    }
done:
    if (wid >= 0)
	winclose(wid);
    if (raster != NULL)
	_TIFFfree(raster);
    if (tif != NULL)
	TIFFClose(tif);
    return (0);
}

static int
prevImage(char* argv[], int ix, int b, int e, int wrap)
{
    int i;

    for (i = ix-1; i >= b && argv[i] == NULL; i--)
	;
    if (i < b) {
	if (wrap) {
	    for (i = e-1; i > ix && argv[i] == NULL; i--)
		;
	} else
	    i = 0;
    }
    return (i);
}

static int
nextImage(char* argv[], int ix, int b, int e, int wrap)
{
    int i;

    for (i = ix+1; i < e && argv[i] == NULL; i++)
	;
    if (i >= e) {
	if (wrap) {
	    for (i = b; i < ix && argv[i] == NULL; i++)
		;
	} else
	    i = 0;
    }
    return (i);
}

static void
beep(void)
{
    greyi(0);
    clear();
    sginap(5);
    lrectwrite(0, 0, width-1, height-1, raster);
}

char* stuff[] = {
"usage: tiffgt [options] file.tif",
"where options are:",
" -c		use colormap visual",
" -d dirnum	set initial directory (default is 0)",
" -e		enable display of TIFF error messages",
" -f  		run program in the foreground",
" -l  		force lsb-to-msb FillOrder",
" -m  		force msb-to-lsb FillOrder",
" -o offset	set initial directory offset",
" -p photo	override photometric interpretation",
" -r		use fullcolor visual",
" -s  		stop decoding on first error (default is ignore errors)",
" -v		enable verbose mode",
" -w		enable display of TIFF warning messages",
NULL
};

static void
usage(void)
{
    char buf[BUFSIZ];
    int i;

    setbuf(stderr, buf);
    for (i = 0; stuff[i] != NULL; i++)
	fprintf(stderr, "%s\n", stuff[i]);
    exit(-1);
}

static uint16
photoArg(const char* arg)
{
    if (strcmp(arg, "miniswhite") == 0)
	return (PHOTOMETRIC_MINISWHITE);
    else if (strcmp(arg, "minisblack") == 0)
	return (PHOTOMETRIC_MINISBLACK);
    else if (strcmp(arg, "rgb") == 0)
	return (PHOTOMETRIC_RGB);
    else if (strcmp(arg, "palette") == 0)
	return (PHOTOMETRIC_PALETTE);
    else if (strcmp(arg, "mask") == 0)
	return (PHOTOMETRIC_MASK);
    else if (strcmp(arg, "separated") == 0)
	return (PHOTOMETRIC_SEPARATED);
    else if (strcmp(arg, "ycbcr") == 0)
	return (PHOTOMETRIC_YCBCR);
    else if (strcmp(arg, "cielab") == 0)
	return (PHOTOMETRIC_CIELAB);
    else if (strcmp(arg, "logl") == 0)
	return (PHOTOMETRIC_LOGL);
    else if (strcmp(arg, "logluv") == 0)
	return (PHOTOMETRIC_LOGLUV);
    else
	return ((uint16) -1);
}

static void
putContigAndDraw(TIFFRGBAImage* img, uint32* raster,
    uint32 x, uint32 y, uint32 w, uint32 h,
    int32 fromskew, int32 toskew,
    unsigned char* cp)
{
    (*putContig)(img, raster, x, y, w, h, fromskew, toskew, cp);
    if (x+w == width) {
	w = width;
	if (img->orientation == ORIENTATION_TOPLEFT)
	    lrectwrite(0, y-(h-1), w-1, y, raster-x-(h-1)*w);
	else
	    lrectwrite(0, y, w-1, y+h-1, raster);
    }
}

static void
putSeparateAndDraw(TIFFRGBAImage* img, uint32* raster,
    uint32 x, uint32 y, uint32 w, uint32 h,
    int32 fromskew, int32 toskew,
    unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a)
{
    (*putSeparate)(img, raster, x, y, w, h, fromskew, toskew, r, g, b, a);
    if (x+w == width) {
	w = width;
	if (img->orientation == ORIENTATION_TOPLEFT)
	    lrectwrite(0, y-(h-1), w-1, y, raster-x-(h-1)*w);
	else
	    lrectwrite(0, y, w-1, y+h-1, raster);
    }
}

/*
 * {red,green,blue}_inverse are tables in libgutil.a that
 * do an inverse map from (r,g,b) to the closest colormap
 * index in the "standard" GL colormap.  grey_inverse is
 * the equivalent map for mapping greyscale values to
 * colormap indices.  We access these maps directly instead
 * of through the rgbi and greyi functions to avoid the
 * additional overhead of the color calls that they make.
 */
extern	u_char red_inverse[256];
extern	u_char green_inverse[256];
extern	u_char blue_inverse[256];
extern	u_char grey_inverse[256];
#define	greyi(g)	grey_inverse[g]

static u_char
rgbi(u_char r, u_char g, u_char b)
{
    return (r == g && g == b ? grey_inverse[r] :
	red_inverse[r] + green_inverse[g] + blue_inverse[b]);
}

/*
 * The following routines move decoded data returned
 * from the TIFF library into rasters that are suitable
 * for passing to lrecwrite.  They do the necessary
 * conversions for when a colormap drawing mode is used.
 */
#define	REPEAT8(op)	REPEAT4(op); REPEAT4(op)
#define	REPEAT4(op)	REPEAT2(op); REPEAT2(op)
#define	REPEAT2(op)	op; op
#define	CASE8(x,op)			\
    switch (x) {			\
    case 7: op; case 6: op; case 5: op;	\
    case 4: op; case 3: op; case 2: op;	\
    case 1: op;				\
    }
#define	CASE4(x,op)	switch (x) { case 3: op; case 2: op; case 1: op; }
#define	NOP

#define	UNROLL8(w, op1, op2) {		\
    uint32 _x;				\
    for (_x = w; _x >= 8; _x -= 8) {	\
	op1;				\
	REPEAT8(op2);			\
    }					\
    if (_x > 0) {			\
	op1;				\
	CASE8(_x,op2);			\
    }					\
}
#define	UNROLL4(w, op1, op2) {		\
    uint32 _x;				\
    for (_x = w; _x >= 4; _x -= 4) {	\
	op1;				\
	REPEAT4(op2);			\
    }					\
    if (_x > 0) {			\
	op1;				\
	CASE4(_x,op2);			\
    }					\
}
#define	UNROLL2(w, op1, op2) {		\
    uint32 _x;				\
    for (_x = w; _x >= 2; _x -= 2) {	\
	op1;				\
	REPEAT2(op2);			\
    }					\
    if (_x) {				\
	op1;				\
	op2;				\
    }					\
}

#define	SKEW(r,g,b,skew)	{ r += skew; g += skew; b += skew; }

#define	DECLAREContigPutFunc(name) \
static void name(\
    TIFFRGBAImage* img, \
    uint32* cp, \
    uint32 x, uint32 y, \
    uint32 w, uint32 h, \
    int32 fromskew, int32 toskew, \
    u_char* pp \
)

#define	DECLARESepPutFunc(name) \
static void name(\
    TIFFRGBAImage* img,\
    uint32* cp,\
    uint32 x, uint32 y, \
    uint32 w, uint32 h,\
    int32 fromskew, int32 toskew,\
    u_char* r, u_char* g, u_char* b, u_char* a\
)

static	tileContigRoutine libput;

/*
 * 8-bit packed samples => colormap
 */
DECLAREContigPutFunc(putcontig8bittile)
{
    int samplesperpixel = img->samplesperpixel;
    TIFFRGBValue* Map = img->Map;

    (void) y;
    fromskew *= samplesperpixel;
    if (Map) {
	while (h-- > 0) {
	    for (x = w; x-- > 0;) {
		*cp++ = rgbi(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
		pp += samplesperpixel;
	    }
	    cp += toskew;
	    pp += fromskew;
	}
    } else {
	while (h-- > 0) {
	    for (x = w; x-- > 0;) {
		*cp++ = rgbi(pp[0], pp[1], pp[2]);
		pp += samplesperpixel;
	    }
	    cp += toskew;
	    pp += fromskew;
	}
    }
}

/*
 * Convert 8-bit packed samples => colormap
 */
DECLAREContigPutFunc(cvtcontig8bittile)
{
    (*libput)(img, cp, x, y, w, h, fromskew, toskew, pp);
    while (h-- > 0) {
	UNROLL8(w, NOP,
	    cp[0] = rgbi(TIFFGetR(cp[0]),TIFFGetG(cp[0]),TIFFGetB(cp[0])); cp++
	);
	cp += toskew;
    }
}

/*
 * 16-bit packed samples => colormap
 */
DECLAREContigPutFunc(putcontig16bittile)
{
    int samplesperpixel = img->samplesperpixel;
    TIFFRGBValue* Map = img->Map;

    (void) y;
    fromskew *= samplesperpixel;
    if (Map) {
	while (h-- > 0) {
	    for (x = w; x-- > 0;) {
		*cp++ = rgbi(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
		pp += samplesperpixel;
	    }
	    cp += toskew;
	    pp += fromskew;
	}
    } else {
	while (h-- > 0) {
	    for (x = w; x-- > 0;) {
		*cp++ = rgbi(pp[0], pp[1], pp[2]);
		pp += samplesperpixel;
	    }
	    cp += toskew;
	    pp += fromskew;
	}
    }
}

/*
 * 8-bit unpacked samples => colormap
 */
DECLARESepPutFunc(putseparate8bittile)
{
    TIFFRGBValue* Map = img->Map;

    (void) y; (void) a;
    if (Map) {
	while (h-- > 0) {
	    for (x = w; x-- > 0;)
		*cp++ = rgbi(Map[*r++], Map[*g++], Map[*b++]);
	    SKEW(r, g, b, fromskew);
	    cp += toskew;
	}
    } else {
	while (h-- > 0) {
	    for (x = w; x-- > 0;)
		*cp++ = rgbi(*r++, *g++, *b++);
	    SKEW(r, g, b, fromskew);
	    cp += toskew;
	}
    }
}

/*
 * 16-bit unpacked samples => colormap
 */
DECLARESepPutFunc(putseparate16bittile)
{
    TIFFRGBValue* Map = img->Map;

    (void) y; (void) a;
    if (Map) {
	while (h-- > 0) {
	    for (x = 0; x < w; x++)
		*cp++ = rgbi(Map[*r++], Map[*g++], Map[*b++]);
	    SKEW(r, g, b, fromskew);
	    cp += toskew;
	}
    } else {
	while (h-- > 0) {
	    for (x = 0; x < w; x++)
		*cp++ = rgbi(*r++, *g++, *b++);
	    SKEW(r, g, b, fromskew);
	    cp += toskew;
	}
    }
}

/*
 * 8-bit packed CMYK samples => cmap
 *
 * NB: The conversion of CMYK->RGB is *very* crude.
 */
DECLAREContigPutFunc(putcontig8bitCMYKtile)
{
    int samplesperpixel = img->samplesperpixel;
    TIFFRGBValue* Map = img->Map;
    uint16 r, g, b, k;

    (void) y;
    fromskew *= samplesperpixel;
    if (Map) {
	while (h-- > 0) {
	    for (x = w; x-- > 0;) {
		k = 255 - pp[3];
		r = (k*(255-pp[0]))/255;
		g = (k*(255-pp[1]))/255;
		b = (k*(255-pp[2]))/255;
		*cp++ = rgbi(Map[r], Map[g], Map[b]);
		pp += samplesperpixel;
	    }
	    pp += fromskew;
	    cp += toskew;
	}
    } else {
	while (h-- > 0) {
	    UNROLL8(w, NOP,
		k = 255 - pp[3];
		r = (k*(255-pp[0]))/255;
		g = (k*(255-pp[1]))/255;
		b = (k*(255-pp[2]))/255;
		*cp++ = rgbi(r, g, b);
		pp += samplesperpixel);
	    cp += toskew;
	    pp += fromskew;
	}
    }
}

#define	YCbCrtoRGB(dst, yc) {						\
    int Y = (yc);							\
    dst = rgbi(								\
	clamptab[Y+Crrtab[Cr]],						\
	clamptab[Y + (int)((Cbgtab[Cb]+Crgtab[Cr])>>16)],		\
	clamptab[Y+Cbbtab[Cb]]);					\
}
#define	YCbCrSetup							\
    TIFFYCbCrToRGB* ycbcr = img->ycbcr;					\
    int* Crrtab = ycbcr->Cr_r_tab;					\
    int* Cbbtab = ycbcr->Cb_b_tab;					\
    int32* Crgtab = ycbcr->Cr_g_tab;					\
    int32* Cbgtab = ycbcr->Cb_g_tab;					\
    TIFFRGBValue* clamptab = ycbcr->clamptab

/*
 * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
 */
DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
{
    YCbCrSetup;
    uint32* cp1 = cp+w+toskew;
    int32 incr = 2*toskew+w;

    (void) y;
    /* XXX adjust fromskew */
    for (; h >= 2; h -= 2) {
	x = w>>1;
	do {
	    int Cb = pp[4];
	    int Cr = pp[5];

	    YCbCrtoRGB(cp [0], pp[0]);
	    YCbCrtoRGB(cp [1], pp[1]);
	    YCbCrtoRGB(cp1[0], pp[2]);
	    YCbCrtoRGB(cp1[1], pp[3]);

	    cp += 2, cp1 += 2;
	    pp += 6;
	} while (--x);
	cp += incr, cp1 += incr;
	pp += fromskew;
    }
}
#undef	YCbCrSetup
#undef	YCbCrtoRGB

/*
 * Setup to handle conversion for display in a colormap
 * window.  Many cases are handled by massaging the mapping
 * tables used by the normal library code to convert 32-bit
 * packed RGBA samples into colormap indices.  Other cases
 * are handled with special-case routines that replace the
 * normal ``put routine'' installed by the library.
 */
static void
setupColormapSupport(TIFFRGBAImage* img)
{
    int bitspersample = img->bitspersample;
    int i;

    if (img->BWmap) {
	i = 255;
	do {
	    uint32* p = img->BWmap[i];
	    switch (bitspersample) {
#define	GREY(x)	p[x] = greyi(TIFFGetR(p[x]))
	    case 1: GREY(7); GREY(6); GREY(5); GREY(4);
	    case 2: GREY(3); GREY(2);
	    case 4: GREY(1);
	    case 8: GREY(0);
	    }
#undef	GREY
	} while (i--);
    } else if (img->PALmap) {
	i = 255;
	do {
	    uint32 rgb;
	    uint32* p = img->PALmap[i];
#define	CMAP(x) \
    (rgb = p[x], p[x] = rgbi(TIFFGetR(rgb),TIFFGetG(rgb),TIFFGetB(rgb)))
	    switch (bitspersample) {
	    case 1: CMAP(7); CMAP(6); CMAP(5); CMAP(4);
	    case 2: CMAP(3); CMAP(2);
	    case 4: CMAP(1);
	    case 8: CMAP(0);
	    }
#undef CMAP
	} while (i--);
    } else if (img->isContig) {
	switch (img->photometric) {
	case PHOTOMETRIC_RGB:
	case PHOTOMETRIC_LOGLUV:
		switch (bitspersample) {
		case 8:  img->put.contig = putcontig8bittile; break;
		case 16: img->put.contig = putcontig16bittile; break;
		}
		break;
	case PHOTOMETRIC_SEPARATED:
		switch (bitspersample) {
		case 8:  img->put.contig = putcontig8bitCMYKtile; break;
		}
		break;
	case PHOTOMETRIC_YCBCR:
		if (img->bitspersample == 8) {
		    uint16 hs, vs;
		    TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING,
			&hs, &vs);
		    switch ((hs<<4)|vs) {
		    case 0x22:			/* most common case */
			img->put.contig = putcontig8bitYCbCr22tile;
			break;
		    default:			/* all others cost more */
			libput = img->put.contig;
			img->put.contig = cvtcontig8bittile;
			break;
		    }
		}
		break;
	}
    } else {
	switch (img->photometric) {
	case PHOTOMETRIC_RGB:
	    switch (img->bitspersample) {
	    case 8:  img->put.separate = putseparate8bittile; break;
	    case 16: img->put.separate = putseparate16bittile; break;
	    }
	    break;
	}
    }
}