This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvtiff.c
2168 lines (1872 loc) · 55.8 KB
/
xvtiff.c
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
/*
* xvtiff.c - load routine for 'TIFF' format pictures
*
* LoadTIFF(fname, numcols, quick) - load a TIFF file
*/
#ifndef va_start
# define NEEDSARGS
#endif
#include <string.h>
#include "xv.h"
#ifdef HAVE_TIFF
#include "tiffio.h" /* has to be after xv.h, as it needs varargs/stdarg */
/* Portions fall under the following copyright:
*
* Copyright (c) 1992, 1993, 1994 Sam Leffler
* Copyright (c) 1992, 1993, 1994 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.
*/
static int copyTiff PARM((TIFF *, char *));
static int cpStrips PARM((TIFF *, TIFF *));
static int cpTiles PARM((TIFF *, TIFF *));
static byte *loadPalette PARM((TIFF *, uint32, uint32, int, int, PICINFO *));
static byte *loadColor PARM((TIFF *, uint32, uint32, int, int, PICINFO *));
static int loadImage PARM((TIFF *, uint32, uint32, byte *, int));
static void _TIFFerr PARM((const char *, const char *, va_list));
static void _TIFFwarn PARM((const char *, const char *, va_list));
static long filesize;
static byte *rmap, *gmap, *bmap;
static const char *filename;
static int error_occurred;
/*******************************************/
int LoadTIFF(fname, pinfo, quick)
char *fname;
PICINFO *pinfo;
int quick;
/*******************************************/
{
/* returns '1' on success, '0' on failure */
TIFF *tif;
uint32 w, h;
float xres, yres;
short bps, spp, photo, orient;
FILE *fp;
byte *pic8;
char *desc, oldpath[MAXPATHLEN+1], tmppath[MAXPATHLEN+1], *sp;
char tmp[256], tmpname[256];
int i, nump;
error_occurred = 0;
pinfo->type = PIC8;
TIFFSetErrorHandler(_TIFFerr);
TIFFSetWarningHandler(_TIFFwarn);
/* open the stream to find out filesize (for info box) */
fp = fopen(fname,"r");
if (!fp) {
TIFFError("LoadTIFF()", "couldn't open file");
return 0;
}
fseek(fp, 0L, 2);
filesize = ftell(fp);
fclose(fp);
rmap = pinfo->r; gmap = pinfo->g; bmap = pinfo->b;
/* a kludge: temporarily cd to the directory that the file is in (if
the file has a path), and cd back when done, as I can't make the
various TIFF error messages print the simple filename */
filename = fname; /* use fullname unless it all works out */
oldpath[0] = '\0';
if (fname[0] == '/') {
xv_getwd(oldpath, sizeof(oldpath));
strcpy(tmppath, fname);
sp = (char *) BaseName(tmppath); /* intentionally losing constness */
if (sp != tmppath) {
sp[-1] = '\0'; /* truncate before last '/' char */
if (chdir(tmppath)) {
oldpath[0] = '\0';
}
else filename = BaseName(fname);
}
}
nump = 1;
if (!quick) {
/* see if there's more than 1 image in tiff file, to determine if we
should do multi-page thing... */
tif = TIFFOpen(filename, "r");
if (!tif) return 0;
while (TIFFReadDirectory(tif)) ++nump;
TIFFClose(tif);
if (DEBUG)
fprintf(stderr,"LoadTIFF: %d page%s found\n", nump, nump==1 ? "" : "s");
/* if there are multiple images, copy them out to multiple tmp files,
and load the first one... */
if (nump>1) {
TIFF *in;
/* GRR 20050320: converted this fake mktemp() to use mktemp()/mkstemp()
internally (formerly it simply prepended tmpdir to the string and
returned immediately) */
xv_mktemp(tmpname, "xvpgXXXXXX");
if (tmpname[0] == '\0') { /* mktemp() or mkstemp() blew up */
sprintf(dummystr,"LoadTIFF: Unable to create temporary filename???");
ErrPopUp(dummystr, "\nHow unlikely!");
return 0;
}
/* GRR 20070506: could clean up unappended tmpname-file here (Linux
bug?), but "cleaner" (more general) to do so in KillPageFiles() */
in = TIFFOpen(filename, "r");
if (!in) return 0;
for (i=1; i<=nump; i++) {
sprintf(tmp, "%s%d", tmpname, i);
if (!copyTiff(in, tmp)) {
SetISTR(ISTR_WARNING, "LoadTIFF: Error writing page files!");
break;
}
if (!TIFFReadDirectory(in)) break;
}
TIFFClose(in);
if (DEBUG)
fprintf(stderr,"LoadTIFF: %d page%s written\n",
i-1, (i-1)==1 ? "" : "s");
sprintf(tmp, "%s%d", tmpname, 1); /* start with page #1 */
filename = tmp;
}
} /* if (!quick) ... */
tif = TIFFOpen(filename, "r");
if (!tif) return 0;
/* flip orientation so that image comes in X order */
TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &orient);
switch (orient) {
case ORIENTATION_TOPLEFT:
case ORIENTATION_TOPRIGHT:
case ORIENTATION_LEFTTOP:
case ORIENTATION_RIGHTTOP: orient = ORIENTATION_BOTLEFT; break;
case ORIENTATION_BOTRIGHT:
case ORIENTATION_BOTLEFT:
case ORIENTATION_RIGHTBOT:
case ORIENTATION_LEFTBOT: orient = ORIENTATION_TOPLEFT; break;
}
TIFFSetField(tif, TIFFTAG_ORIENTATION, orient);
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bps);
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo);
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &spp);
if ((TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres) == 1) &&
(TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres) == 1)) {
normaspect = yres / xres;
if (DEBUG) fprintf(stderr,"TIFF aspect = %f\n", normaspect);
}
if (spp == 1) {
pic8 = loadPalette(tif, w, h, photo, bps, pinfo);
} else {
pic8 = loadColor(tif, w, h, photo, bps, pinfo);
}
/* try to get comments, if any */
pinfo->comment = (char *) NULL;
desc = (char *) NULL;
TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &desc);
if (desc && strlen(desc) > (size_t) 0) {
/* kludge: tiff library seems to return bizarre comments */
if (strlen(desc)==4 && strcmp(desc, "\367\377\353\370")==0) {}
else {
pinfo->comment = (char *) malloc(strlen(desc) + 1);
if (pinfo->comment) strcpy(pinfo->comment, desc);
}
}
TIFFClose(tif);
/* un-kludge */
if (oldpath[0] != '\0') chdir(oldpath);
if (error_occurred) {
if (pic8) free(pic8);
if (pinfo->comment) free(pinfo->comment);
pinfo->comment = (char *) NULL;
if (!quick && nump>1) KillPageFiles(tmpname, nump);
SetCursors(-1);
return 0;
}
pinfo->pic = pic8;
pinfo->w = w; pinfo->h = h;
pinfo->normw = pinfo->w; pinfo->normh = pinfo->h;
pinfo->frmType = F_TIFF;
if (nump>1) strcpy(pinfo->pagebname, tmpname);
pinfo->numpages = nump;
if (pinfo->pic) return 1;
/* failed. if we malloc'd a comment, free it */
if (pinfo->comment) free(pinfo->comment);
pinfo->comment = (char *) NULL;
if (!quick && nump>1) KillPageFiles(tmpname, nump);
SetCursors(-1);
return 0;
}
/*******************************************/
#define CopyField(tag, v) \
if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
#define CopyField2(tag, v1, v2) \
if (TIFFGetField(in, tag, &v1, &v2)) TIFFSetField(out, tag, v1, v2)
#define CopyField3(tag, v1, v2, v3) \
if (TIFFGetField(in, tag, &v1, &v2, &v3)) TIFFSetField(out, tag, v1, v2, v3)
/*******************************************/
static int copyTiff(in, fname)
TIFF *in;
char *fname;
{
/* copies tiff (sub)image to given filename. (Used only for multipage
images.) Returns 0 on error */
TIFF *out;
short bitspersample, samplesperpixel, shortv, *shortav;
uint32 w, l;
float floatv, *floatav;
char *stringv;
uint32 longv;
uint16 *red, *green, *blue, shortv2;
int rv;
out = TIFFOpen(fname, "w");
if (!out) return 0;
if (TIFFGetField(in, TIFFTAG_COMPRESSION, &shortv)){
/* Currently, the TIFF Library cannot correctly copy TIFF version 6.0 (or
* earlier) files that use "old" JPEG compression, so don't even try. */
if (shortv == COMPRESSION_OJPEG) return 0;
TIFFSetField(out, TIFFTAG_COMPRESSION, shortv);
}
CopyField (TIFFTAG_SUBFILETYPE, longv);
CopyField (TIFFTAG_TILEWIDTH, w);
CopyField (TIFFTAG_TILELENGTH, l);
CopyField (TIFFTAG_IMAGEWIDTH, w);
CopyField (TIFFTAG_IMAGELENGTH, l);
CopyField (TIFFTAG_BITSPERSAMPLE, bitspersample);
CopyField (TIFFTAG_PREDICTOR, shortv);
CopyField (TIFFTAG_PHOTOMETRIC, shortv);
CopyField (TIFFTAG_THRESHHOLDING, shortv);
CopyField (TIFFTAG_FILLORDER, shortv);
CopyField (TIFFTAG_ORIENTATION, shortv);
CopyField (TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
CopyField (TIFFTAG_MINSAMPLEVALUE, shortv);
CopyField (TIFFTAG_MAXSAMPLEVALUE, shortv);
CopyField (TIFFTAG_XRESOLUTION, floatv);
CopyField (TIFFTAG_YRESOLUTION, floatv);
CopyField (TIFFTAG_GROUP3OPTIONS, longv);
CopyField (TIFFTAG_GROUP4OPTIONS, longv);
CopyField (TIFFTAG_RESOLUTIONUNIT, shortv);
CopyField (TIFFTAG_PLANARCONFIG, shortv);
CopyField (TIFFTAG_ROWSPERSTRIP, longv);
CopyField (TIFFTAG_XPOSITION, floatv);
CopyField (TIFFTAG_YPOSITION, floatv);
CopyField (TIFFTAG_IMAGEDEPTH, longv);
CopyField (TIFFTAG_TILEDEPTH, longv);
CopyField2(TIFFTAG_EXTRASAMPLES, shortv, shortav);
CopyField3(TIFFTAG_COLORMAP, red, green, blue);
CopyField2(TIFFTAG_PAGENUMBER, shortv, shortv2);
CopyField (TIFFTAG_ARTIST, stringv);
CopyField (TIFFTAG_IMAGEDESCRIPTION, stringv);
CopyField (TIFFTAG_MAKE, stringv);
CopyField (TIFFTAG_MODEL, stringv);
CopyField (TIFFTAG_SOFTWARE, stringv);
CopyField (TIFFTAG_DATETIME, stringv);
CopyField (TIFFTAG_HOSTCOMPUTER, stringv);
CopyField (TIFFTAG_PAGENAME, stringv);
CopyField (TIFFTAG_DOCUMENTNAME, stringv);
CopyField2(TIFFTAG_JPEGTABLES, longv, stringv);
CopyField (TIFFTAG_YCBCRCOEFFICIENTS, floatav);
CopyField2(TIFFTAG_YCBCRSUBSAMPLING, shortv,shortv2);
CopyField (TIFFTAG_YCBCRPOSITIONING, shortv);
CopyField (TIFFTAG_REFERENCEBLACKWHITE, floatav);
if (TIFFIsTiled(in)) rv = cpTiles (in, out);
else rv = cpStrips(in, out);
TIFFClose(out);
return rv;
}
/*******************************************/
static int cpStrips(in, out)
TIFF *in, *out;
{
tsize_t bufsize;
byte *buf;
bufsize = TIFFStripSize(in);
if (bufsize <= 0) return 0; /* tsize_t is signed */
buf = (byte *) malloc((size_t) bufsize);
if (buf) {
tstrip_t s, ns = TIFFNumberOfStrips(in);
uint32 *bytecounts;
TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
for (s = 0; s < ns; s++) {
if (bytecounts[s] > bufsize) {
buf = (unsigned char *) realloc(buf, (size_t) bytecounts[s]);
if (!buf) return (0);
bufsize = bytecounts[s];
}
if (TIFFReadRawStrip (in, s, buf, (tsize_t) bytecounts[s]) < 0 ||
TIFFWriteRawStrip(out, s, buf, (tsize_t) bytecounts[s]) < 0) {
free(buf);
return 0;
}
}
free(buf);
return 1;
}
return 0;
}
/*******************************/
static int cpTiles(in, out)
TIFF *in, *out;
{
tsize_t bufsize;
byte *buf;
bufsize = TIFFTileSize(in);
if (bufsize <= 0) return 0; /* tsize_t is signed */
buf = (unsigned char *) malloc((size_t) bufsize);
if (buf) {
ttile_t t, nt = TIFFNumberOfTiles(in);
uint32 *bytecounts;
TIFFGetField(in, TIFFTAG_TILEBYTECOUNTS, &bytecounts);
for (t = 0; t < nt; t++) {
if (bytecounts[t] > bufsize) {
buf = (unsigned char *)realloc(buf, (size_t) bytecounts[t]);
if (!buf) return (0);
bufsize = bytecounts[t];
}
if (TIFFReadRawTile (in, t, buf, (tsize_t) bytecounts[t]) < 0 ||
TIFFWriteRawTile(out, t, buf, (tsize_t) bytecounts[t]) < 0) {
free(buf);
return 0;
}
}
free(buf);
return 1;
}
return 0;
}
/*******************************************/
static byte *loadPalette(tif, w, h, photo, bps, pinfo)
TIFF *tif;
uint32 w,h;
int photo,bps;
PICINFO *pinfo;
{
byte *pic8;
uint32 npixels;
switch (photo) {
case PHOTOMETRIC_PALETTE:
pinfo->colType = F_FULLCOLOR;
sprintf(pinfo->fullInfo, "TIFF, %u-bit, palette format. (%ld bytes)",
bps, filesize);
break;
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
pinfo->colType = (bps==1) ? F_BWDITHER : F_GREYSCALE;
sprintf(pinfo->fullInfo,"TIFF, %u-bit, %s format. (%ld bytes)",
bps,
photo == PHOTOMETRIC_MINISWHITE ? "min-is-white" :
"min-is-black",
filesize);
break;
}
sprintf(pinfo->shrtInfo, "%ux%u TIFF.",(u_int) w, (u_int) h);
npixels = w*h;
if (npixels/w != h) {
/* SetISTR(ISTR_WARNING, "loadPalette() - image dimensions too large"); */
TIFFError(filename, "Image dimensions too large");
return (byte *) NULL;
}
pic8 = (byte *) malloc((size_t) npixels);
if (!pic8) FatalError("loadPalette() - couldn't malloc 'pic8'");
if (loadImage(tif, w, h, pic8, 0)) return pic8;
return (byte *) NULL;
}
/*******************************************/
static byte *loadColor(tif, w, h, photo, bps, pinfo)
TIFF *tif;
uint32 w,h;
int photo,bps;
PICINFO *pinfo;
{
byte *pic24, *pic8;
uint32 npixels, count;
pinfo->colType = F_FULLCOLOR;
sprintf(pinfo->fullInfo, "TIFF, %u-bit, %s format. (%ld bytes)",
bps,
(photo == PHOTOMETRIC_RGB ? "RGB" :
photo == PHOTOMETRIC_YCBCR ? "YCbCr" :
"???"),
filesize);
sprintf(pinfo->shrtInfo, "%ux%u TIFF.",(u_int) w, (u_int) h);
npixels = w*h;
count = 3*npixels;
if (npixels/w != h || count/3 != npixels) {
/* SetISTR(ISTR_WARNING, "loadPalette() - image dimensions too large"); */
TIFFError(filename, "Image dimensions too large");
return (byte *) NULL;
}
/* allocate 24-bit image */
pic24 = (byte *) malloc((size_t) count);
if (!pic24) FatalError("loadColor() - couldn't malloc 'pic24'");
pic8 = (byte *) NULL;
if (loadImage(tif, w, h, pic24, 0)) {
pinfo->type = PIC24;
pic8 = pic24;
}
else free(pic24);
return pic8;
}
/*******************************************/
static void _TIFFerr(module, fmt, ap)
const char *module;
const char *fmt;
va_list ap;
{
char buf[2048];
char *cp = buf;
if (module != NULL) {
sprintf(cp, "%s: ", module);
cp = (char *) index(cp, '\0');
}
vsprintf(cp, fmt, ap);
strcat(cp, ".");
SetISTR(ISTR_WARNING, "%s", buf);
error_occurred = 1;
}
/*******************************************/
static void _TIFFwarn(module, fmt, ap)
const char *module;
const char *fmt;
va_list ap;
{
char buf[2048];
char *cp = buf;
if (module != NULL) {
sprintf(cp, "%s: ", module);
cp = (char *) index(cp, '\0');
}
strcpy(cp, "Warning, ");
cp = (char *) index(cp, '\0');
vsprintf(cp, fmt, ap);
strcat(cp, ".");
SetISTR(ISTR_WARNING, "%s", buf);
}
typedef byte RGBvalue;
static u_short bitspersample;
static u_short samplesperpixel;
static u_short photometric;
static u_short orientation;
/* colormap for pallete images */
static u_short *redcmap, *greencmap, *bluecmap;
static int stoponerr; /* stop on read error */
/* YCbCr support */
static u_short YCbCrHorizSampling;
static u_short YCbCrVertSampling;
static float *YCbCrCoeffs;
static float *refBlackWhite;
static byte **BWmap;
static byte **PALmap;
/* XXX Work around some collisions with the new library. */
#define tileContigRoutine _tileContigRoutine
#define tileSeparateRoutine _tileSeparateRoutine
typedef void (*tileContigRoutine) PARM((byte*, u_char*, RGBvalue*,
uint32, uint32, int, int));
typedef void (*tileSeparateRoutine) PARM((byte*, u_char*, u_char*, u_char*,
RGBvalue*, uint32, uint32, int, int));
static int checkcmap PARM((int, u_short*, u_short*, u_short*));
static int gt PARM((TIFF *, uint32, uint32, byte *));
static uint32 setorientation PARM((TIFF *, uint32));
static int gtTileContig PARM((TIFF *, byte *, RGBvalue *,
uint32, uint32, int));
static int gtTileSeparate PARM((TIFF *, byte *, RGBvalue *,
uint32, uint32, int));
static int gtStripContig PARM((TIFF *, byte *, RGBvalue *,
uint32, uint32, int));
static int gtStripSeparate PARM((TIFF *, byte *, RGBvalue *,
uint32, uint32, int));
static int makebwmap PARM((void));
static int makecmap PARM((void));
static void put8bitcmaptile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put4bitcmaptile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put2bitcmaptile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put1bitcmaptile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void putgreytile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put1bitbwtile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put2bitbwtile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put4bitbwtile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void put16bitbwtile PARM((byte *, u_short *, RGBvalue *,
uint32, uint32, int, int));
static void putRGBcontig8bittile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void putRGBcontig16bittile PARM((byte *, u_short *, RGBvalue *,
uint32, uint32, int, int));
static void putRGBseparate8bittile PARM((byte *, u_char *, u_char *,
u_char *, RGBvalue *,
uint32, uint32, int, int));
static void putRGBseparate16bittile PARM((byte *, u_short *, u_short *,
u_short *, RGBvalue *,
uint32, uint32, int, int));
static void initYCbCrConversion PARM((void));
static void putRGBContigYCbCrClump PARM((byte *, u_char *, int, int,
uint32, int, int, int));
static void putRGBSeparateYCbCrClump PARM((byte *, u_char *, u_char *,
u_char *, int, int, uint32, int,
int, int));
static void putRGBSeparate16bitYCbCrClump PARM((byte *, u_short *, u_short *,
u_short *, int, int, uint32,
int, int, int));
static void putcontig8bitYCbCrtile PARM((byte *, u_char *, RGBvalue *,
uint32, uint32, int, int));
static void putYCbCrseparate8bittile PARM((byte *, u_char *, u_char *,
u_char *, RGBvalue *,
uint32, uint32, int, int));
static void putYCbCrseparate16bittile PARM((byte *, u_short *, u_short *,
u_short *, RGBvalue *,
uint32, uint32, int, int));
static tileContigRoutine pickTileContigCase PARM((RGBvalue *));
static tileSeparateRoutine pickTileSeparateCase PARM((RGBvalue *));
/*******************************************/
static int loadImage(tif, rwidth, rheight, raster, stop)
TIFF *tif;
uint32 rwidth, rheight;
byte *raster;
int stop;
{
int ok;
uint32 width, height;
TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
switch (bitspersample) {
case 1:
case 2:
case 4:
case 8:
case 16: break;
default:
TIFFError(TIFFFileName(tif),
"Sorry, cannot handle %d-bit pictures", bitspersample);
return (0);
}
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
switch (samplesperpixel) {
case 1:
case 3:
case 4: break;
default:
TIFFError(TIFFFileName(tif),
"Sorry, cannot handle %d-channel images", samplesperpixel);
return (0);
}
if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
switch (samplesperpixel) {
case 1: photometric = PHOTOMETRIC_MINISBLACK; break;
case 3:
case 4: photometric = PHOTOMETRIC_RGB; break;
default:
TIFFError(TIFFFileName(tif),
"Missing needed \"PhotometricInterpretation\" tag");
return (0);
}
TIFFWarning(TIFFFileName(tif),
"No \"PhotometricInterpretation\" tag, assuming %s\n",
photometric == PHOTOMETRIC_RGB ? "RGB" : "min-is-black");
}
/* XXX maybe should check photometric? */
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
/* XXX verify rwidth and rheight against width and height */
stoponerr = stop;
BWmap = NULL;
PALmap = NULL;
ok = gt(tif, rwidth, height, raster + (rheight-height)*rwidth);
if (BWmap)
free((char *)BWmap);
if (PALmap)
free((char *)PALmap);
return (ok);
}
/*******************************************/
static int checkcmap(n, r, g, b)
int n;
u_short *r, *g, *b;
{
while (n-- >= 0)
if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) return (16);
TIFFWarning(filename, "Assuming 8-bit colormap");
return (8);
}
/*******************************************/
static int gt(tif, w, h, raster)
TIFF *tif;
uint32 w, h;
byte *raster;
{
#ifdef USE_LIBJPEG_FOR_TIFF_YCbCr_RGB_CONVERSION
u_short compression;
#endif
u_short minsamplevalue, maxsamplevalue, planarconfig;
RGBvalue *Map;
int bpp = 1, e;
int x, range;
#ifdef USE_LIBJPEG_FOR_TIFF_YCbCr_RGB_CONVERSION
TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);
#endif
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE, &minsamplevalue);
TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE, &maxsamplevalue);
Map = NULL;
switch (photometric) {
case PHOTOMETRIC_YCBCR:
#ifdef USE_LIBJPEG_FOR_TIFF_YCbCr_RGB_CONVERSION
if (compression == COMPRESSION_JPEG
#ifdef LIBTIFF_HAS_OLDJPEG_SUPPORT
|| compression == COMPRESSION_OJPEG
#endif
) {
/* FIXME: Remove the following test as soon as TIFF Library is fixed!
* (Currently [June 2002] this requires supporting patches in both
* tif_ojpeg.c and tif_jpeg.c in order to support subsampled YCbCr
* images having separated color planes.) */
if (planarconfig == PLANARCONFIG_CONTIG) {
/* can rely on libjpeg to convert to RGB (assuming newer libtiff,
* compiled with appropriate forms of JPEG support) */
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
photometric = PHOTOMETRIC_RGB;
} else {
TIFFError(filename, "Cannot handle format");
return (0);
}
} else
#endif // USE_LIBJPEG_FOR_TIFF_YCbCr_RGB_CONVERSION
{
TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &YCbCrCoeffs);
TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
&YCbCrHorizSampling, &YCbCrVertSampling);
/* According to the TIFF specification, if no "ReferenceBlackWhite"
* tag is present in the input file, "TIFFGetFieldDefaulted()" returns
* default reference black and white levels suitable for PHOTOMETRIC_RGB;
* namely: <0,255,0,255,0,255>. But for PHOTOMETRIC_YCBCR in JPEG
* images, the usual default (e.g., corresponding to the behavior of the
* IJG libjpeg) is: <0,255,128,255,128,255>. Since libtiff doesn't have
* a clean, standard interface for making this repair, the following
* slightly dirty code installs the default. --Scott Marovich,
* Hewlett-Packard Labs, 9/2001.
*/
if (!TIFFGetField(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite)) {
TIFFGetFieldDefaulted(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite);
refBlackWhite[4] = refBlackWhite[2] = 1 << (bitspersample - 1);
}
TIFFGetFieldDefaulted(tif, TIFFTAG_REFERENCEBLACKWHITE, &refBlackWhite);
initYCbCrConversion();
}
/* fall thru... */
case PHOTOMETRIC_RGB:
bpp *= 3;
if (minsamplevalue == 0 && maxsamplevalue == 255)
break;
/* fall thru... */
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_MINISWHITE:
range = maxsamplevalue - minsamplevalue;
Map = (RGBvalue *)malloc((range + 1) * sizeof (RGBvalue));
if (Map == NULL) {
TIFFError(filename, "No space for photometric conversion table");
return (0);
}
if (photometric == PHOTOMETRIC_MINISWHITE) {
for (x = 0; x <= range; x++)
Map[x] = (255*(range-x))/range;
} else {
for (x = 0; x <= range; x++)
Map[x] = (255*x)/range;
}
if (range<256) {
for (x=0; x<=range; x++) rmap[x] = gmap[x] = bmap[x] = Map[x];
} else {
for (x=0; x<256; x++)
rmap[x] = gmap[x] = bmap[x] = Map[(range*x)/255];
}
if (photometric != PHOTOMETRIC_RGB && bitspersample <= 8) {
/*
* Use photometric mapping table to construct
* unpacking tables for samples <= 8 bits.
*/
if (!makebwmap())
return (0);
/* no longer need Map, free it */
free((char *)Map);
Map = NULL;
}
break;
case PHOTOMETRIC_PALETTE:
if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
&redcmap, &greencmap, &bluecmap)) {
TIFFError(filename, "Missing required \"Colormap\" tag");
return (0);
}
/*
* Convert 16-bit colormap to 8-bit (unless it looks
* like an old-style 8-bit colormap).
*/
range = (1<<bitspersample)-1;
if (checkcmap(range, redcmap, greencmap, bluecmap) == 16) {
#define CVT(x) ((((int) x) * 255) / ((1L<<16)-1))
for (x = range; x >= 0; x--) {
rmap[x] = CVT(redcmap[x]);
gmap[x] = CVT(greencmap[x]);
bmap[x] = CVT(bluecmap[x]);
}
} else {
for (x = range; x >= 0; x--) {
rmap[x] = redcmap[x];
gmap[x] = greencmap[x];
bmap[x] = bluecmap[x];
}
}
if (bitspersample <= 8) {
/*
* Use mapping table to construct
* unpacking tables for samples < 8 bits.
*/
if (!makecmap())
return (0);
}
break;
default:
TIFFError(filename, "Unknown photometric tag %u", photometric);
return (0);
}
if (planarconfig == PLANARCONFIG_SEPARATE && samplesperpixel > 1) {
e = TIFFIsTiled(tif) ? gtTileSeparate (tif, raster, Map, h, w, bpp) :
gtStripSeparate(tif, raster, Map, h, w, bpp);
} else {
e = TIFFIsTiled(tif) ? gtTileContig (tif, raster, Map, h, w, bpp) :
gtStripContig(tif, raster, Map, h, w, bpp);
}
if (Map) free((char *)Map);
return (e);
}
/*******************************************/
static uint32 setorientation(tif, h)
TIFF *tif;
uint32 h;
{
/* note that orientation was flipped in LoadTIFF() (near line 175) */
uint32 y;
TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &orientation);
switch (orientation) {
case ORIENTATION_BOTRIGHT:
case ORIENTATION_RIGHTBOT: /* XXX */
case ORIENTATION_LEFTBOT: /* XXX */
TIFFWarning(filename, "using bottom-left orientation");
orientation = ORIENTATION_BOTLEFT;
/* fall thru... */
case ORIENTATION_BOTLEFT:
y = 0;
break;
case ORIENTATION_TOPRIGHT:
case ORIENTATION_RIGHTTOP: /* XXX */
case ORIENTATION_LEFTTOP: /* XXX */
default:
TIFFWarning(filename, "using top-left orientation");
orientation = ORIENTATION_TOPLEFT;
/* fall thru... */
case ORIENTATION_TOPLEFT:
/* GRR 20050319: This may be wrong for tiled images (also stripped?);
* looks like we want to return th-1 instead of h-1 in at least some
* cases. For now, just added quick hack (USE_TILED_TIFF_BOTLEFT_FIX)
* to gtTileContig(). (Note that, as of libtiff 3.7.1, tiffcp still
* has exactly the same bug.) */
y = h-1;
break;
}
return (y);
}
/*
* Get a tile-organized image that has
* PlanarConfiguration contiguous if SamplesPerPixel > 1
* or
* SamplesPerPixel == 1
*/
/*******************************************/
static int gtTileContig(tif, raster, Map, h, w, bpp)
TIFF *tif;
byte *raster;
RGBvalue *Map;
uint32 h, w;
int bpp;
{
uint32 col, row, y;
uint32 tw, th;
u_char *buf;
int fromskew, toskew;
u_int nrow;
tileContigRoutine put;
tsize_t bufsize;
put = pickTileContigCase(Map);
if (put == 0) return (0);
bufsize = TIFFTileSize(tif);
if (bufsize <= 0) return 0; /* tsize_t is signed */
buf = (u_char *) malloc((size_t) bufsize);
if (buf == 0) {
TIFFError(filename, "No space for tile buffer");