forked from MalcolmMcLean/tiffloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadtiff.c
4277 lines (3742 loc) · 110 KB
/
loadtiff.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
/*
TIFF loader, by Malcolm McLean
Meant to be a pretty comprehensive, one file, portable TIFF reader
We just read everything in as 8 bit RGBs.
Current limitations - no JPEG, no alpha, a few odd formats still
unsupported
No sophisticated colour handling
Free for public use
Acknowlegements, Lode Vandevenne for the Zlib decompressor
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#define TAG_BYTE 1
#define TAG_ASCII 2
#define TAG_SHORT 3
#define TAG_LONG 4
#define TAG_RATIONAL 5
/* data types
1 BYTE 8 - bit unsigned integer
2 ASCII 8 - bit, NULL - terminated string
3 SHORT 16 - bit unsigned integer
4 LONG 32 - bit unsigned integer
5 RATIONAL Two 32 - bit unsigned integers
6 SBYTE 8 - bit signed integer
7 UNDEFINE 8 - bit byte
8 SSHORT 16 - bit signed integer
9 SLONG 32 - bit signed integer
10 SRATIONAL Two 32 - bit signed integers
11 FLOAT 4 - byte single - precision IEEE floating - point value
12 DOUBLE 8 - byte double - precision IEEE floating - point value
*/
#define COMPRESSION_NONE 1
#define COMPRESSION_CCITTRLE 2
#define COMPRESSION_CCITTFAX3 3
#define COMPRESSION_CCITT_T4 3
#define COMPRESSION_CCITTFAX4 4
#define COMPRESSION_CCITT_T6 4
#define COMPRESSION_LZW 5
#define COMPRESSION_OJPEG 6
#define COMPRESSION_JPEG 7
#define COMPRESSION_NEXT 32766
#define COMPRESSION_CCITTRLEW 32771
#define COMPRESSION_PACKBITS 32773
#define COMPRESSION_THUNDERSCAN 32809
#define COMPRESSION_IT8CTPAD 32895
#define COMPRESSION_IT8LW 32896
#define COMPRESSION_IT8MP 32897
#define COMPRESSION_IT8BL 32898
#define COMPRESSION_PIXARFILM 32908
#define COMPRESSION_PIXARLOG 32909
#define COMPRESSION_DEFLATE 32946
#define COMPRESSION_ADOBE_DEFLATE 8
#define COMPRESSION_DCS 32947
#define COMPRESSION_JBIG 34661
#define COMPRESSION_SGILOG 34676
#define COMPRESSION_SGILOG24 34677
#define COMPRESSION_JP2000 34712
#define PI_WhiteIsZero 0
#define PI_BlackIsZero 1
#define PI_RGB 2
#define PI_RGB_Palette 3
#define PI_Tranparency_Mask 4
#define PI_CMYK 5
#define PI_YCbCr 6
#define PI_CIELab 8
#define TID_IMAGEWIDTH 256
#define TID_IMAGEHEIGHT 257
#define TID_BITSPERSAMPLE 258
#define TID_COMPRESSION 259
#define TID_PHOTOMETRICINTERPRETATION 262
#define TID_FILLORDER 266
#define TID_STRIPOFFSETS 273
#define TID_SAMPLESPERPIXEL 277
#define TID_ROWSPERSTRIP 278
#define TID_STRIPBYTECOUNTS 279
#define TID_PLANARCONFIGUATION 284
#define TID_T4OPTIONS 292
#define TID_PREDICTOR 317
#define TID_COLORMAP 320
#define TID_TILEWIDTH 322
#define TID_TILELENGTH 323
#define TID_TILEOFFSETS 324
#define TID_TILEBYTECOUNTS 325
#define TID_SAMPLEFORMAT 339
#define TID_SMINSAMPLEVALUE 340
#define TID_SMAXSAMPLEVALUE 341
#define TID_YCBCRCOEFFICIENTS 529
#define TID_YCBCRSUBSAMPLING 530
#define TID_YCBCRPOSITIONING 531
#define SAMPLEFORMAT_UINT 1
#define SAMPLEFORMAT_INT 2
#define SAMPLEFORMAT_IEEEFP 3
#define SAMPLEFORMAT_VOID 4
//#define SAMPLEFORMAT_COMPLEXINT 5
//#define SAMPLEFORMAT_COMPLEXIEEEFP 6
/* Artist 315 ASCII - * *
BadFaxLines[1] 326 SHORT or LONG - - -
BitsPerSample 258 SHORT * * *
CellLength 265 SHORT * * *
CellWidth 264 SHORT * * *
CleanFaxData[1] 327 SHORT - - -
ColorMap 320 SHORT - * *
ColorResponseCurve 301 SHORT * * x
ColorResponseUnit 300 SHORT * x x
Compression 259 SHORT * * *
Uncompressed 1 * * *
CCITT 1D 2 * * *
CCITT Group 3 3 * * *
CCITT Group 4 4 * * *
LZW 5 - * *
JPEG 6 - - *
Uncompressed 32771 * x x
Packbits 32773 * * *
ConsecutiveBadFaxLines[1] 328 LONG or SHORT - - -
Copyright 33432 ASCII - - *
DateTime 306 ASCII - * *
DocumentName 269 ASCII * * *
DotRange 336 BYTE or SHORT - - *
ExtraSamples 338 BYTE - - *
FillOrder 266 SHORT * * *
FreeByteCounts 289 LONG * * *
FreeOffsets 288 LONG * * *
GrayResponseCurve 291 SHORT * * *
GrayResponseUnit 290 SHORT * * *
HalftoneHints 321 SHORT - - *
HostComputer 316 ASCII - * *
ImageDescription 270 ASCII * * *
ImageHeight 257 SHORT or LONG * * *
ImageWidth 256 SHORT or LONG * * *
InkNames 333 ASCII - - *
InkSet 332 SHORT - - *
JPEGACTTables 521 LONG - - *
JPEGDCTTables 520 LONG - - *
JPEGInterchangeFormat 513 LONG - - *
JPEGInterchangeFormatLength 514 LONG - - *
JPEGLosslessPredictors 517 SHORT - - *
JPEGPointTransforms 518 SHORT - - *
JPEGProc 512 SHORT - - *
JPEGRestartInterval 515 SHORT - - *
JPEGQTables 519 LONG - - *
Make 271 ASCII * * *
MaxSampleValue 281 SHORT * * *
MinSampleValue 280 SHORT * * *
Model 272 ASCII * * *
NewSubFileType 254 LONG - * *
NumberOfInks 334 SHORT - - *
Orientation 274 SHORT * * *
PageName 285 ASCII * * *
PageNumber 297 SHORT * * *
PhotometricInterpretation 262 SHORT * * *
WhiteIsZero 0 * * *
BlackIsZero 1 * * * RGB 2 * * *
RGB Palette 3 - * *
Tranparency Mask 4 - - *
CMYK 5 - - *
YCbCr 6 - - *
CIELab 8 - - *
PlanarConfiguration 284 SHORT * * *
Predictor 317 SHORT - * *
PrimaryChromaticities 319 RATIONAL - * *
ReferenceBlackWhite 532 LONG - - *
ResolutionUnit 296 SHORT * * *
RowsPerStrip 278 SHORT or LONG * * *
SampleFormat 339 SHORT - - *
SamplesPerPixel 277 SHORT * * *
SMaxSampleValue 341 Any - - *
SMinSampleValue 340 Any - - *
Software 305 ASCII - * *
StripByteCounts 279 LONG or SHORT * * *
StripOffsets 273 SHORT or LONG * * *
SubFileType 255 SHORT * x x
T4Options[2] 292 LONG * * *
T6Options[3] 293 LONG * * *
TargetPrinter 337 ASCII - - *
Thresholding 263 SHORT * * *
TileByteCounts 325 SHORT or LONG - - *
TileLength 323 SHORT or LONG - - *
TileOffsets 324 LONG - - *
TileWidth 322 SHORT or LONG - - *
TransferFunction[4] 301 SHORT - - *
TransferRange 342 SHORT - - *
XPosition 286 RATIONAL * * *
XResolution 282 RATIONAL * * *
YCbCrCoefficients 529 RATIONAL - - *
YCbCrPositioning 531 SHORT - - *
YCbCrSubSampling 530 SHORT - - *
YPosition 287 RATIONAL * * *
YResolution 283 RATIONAL * * *
WhitePoint 318 RATIONAL - * *
*/
typedef struct basic_header
{
unsigned long newsubfiletype;
int imagewidth;
int imageheight;
int bitspersample[16];
int compression;
int fillorder;
int photometricinterpretation;
unsigned long *stripoffsets;
int Nstripoffsets;
int samplesperpixel;
int rowsperstrip;
unsigned long *stripbytecounts;
int Nstripbytecounts;
double xresolution;
double yresolution;
int resolutionunit;
/* Palette files */
unsigned char *colormap;
int Ncolormap;
/* RGB */
int planarconfiguration;
int predictor;
/* YCbCr*/
//double YCbCrCoefficients[3];
double LumaRed;
double LumaGreen;
double LumaBlue;
int YCbCrSubSampling_h;
int YCbCrSubSampling_v;
int YCbCrPositioning;
int ReferenceBlackWhite;
/* Class F */
int BadFaxLines;
int CleanFaxData;
int ConsecutiveBadFaxLines;
unsigned long T4options;
/* tiling */
int tilewidth;
int tileheight;
unsigned long *tileoffsets;
int Ntileoffsets;
unsigned long *tilebytecounts;
int Ntilebytecounts;
/* Malcolm easier to support this now*/
int sampleformat[16];
double *smaxsamplevalue;
int Nsmaxsamplevalue;
double *sminsamplevalue;
int Nsminsamplevalue;
int endianness;
} BASICHEADER;
struct tifftag
{
unsigned short tagid;
unsigned short datatype;
unsigned long datacount;
unsigned long dataoffset;
};
typedef struct
{
unsigned short tagid;
unsigned short datatype;
unsigned long datacount;
double scalar;
char *ascii;
void *vector;
int bad;
} TAG;
typedef struct
{
unsigned char *data;
int N;
int pos;
int bit;
int endianness;
} BSTREAM;
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 1
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 2
#endif
static void header_defaults(BASICHEADER *header);
static void freeheader(BASICHEADER *header);
static int header_fixupsections(BASICHEADER *header);
static int header_not_ok(BASICHEADER *header);
static int fillheader(BASICHEADER *header, TAG *tags, int Ntags);
static unsigned char *decompress(FILE *fp, unsigned long count, int compression, unsigned long *Nret, int width, int height, unsigned long T4options );
static void header_defaults(BASICHEADER *header);
static TAG *floadheader(int type, FILE *fp, int *Ntags);
static void killtags(TAG *tags, int N);
static int floadtag(TAG *tag, int type, FILE *fp);
static double tag_getentry(TAG *tag, int index);
static unsigned char *loadraster(BASICHEADER *header, FILE *fp);
static unsigned char *readstrip(BASICHEADER *header, int index, int *strip_width, int *strip_height, FILE *fp);
static unsigned char *readtile(BASICHEADER *header, int index, int *tile_width, int *tile_height, FILE *fp);
static unsigned char *readchannel(BASICHEADER *header, int index, int *channel_width, int *channel_height, FILE *fp);
static BSTREAM *bstream(unsigned char *data, int N, int endinaness);
static void killbstream(BSTREAM *bs);
static int getbit(BSTREAM *bs);
static int getbits(BSTREAM *bs, int nbits);
static int synchtobyte(BSTREAM *bs);
static int writebit(BSTREAM *bs, int bit);
static void rgbapaste(unsigned char *rgba, int width, int height, unsigned char *tile, int twidth, int theight, int x, int y);
static long fget32(int type, FILE *fp);
static int fget16(int type, FILE *fp);
static long fget32be(FILE *fp);
static int fget16be(FILE *fp);
static long fget32le(FILE *fp);
static int fget16le(FILE *fp);
static unsigned long fget32u(int type, FILE *fp);
static unsigned int fget16u(int type, FILE *fp);
static char *freadasciiz(FILE *fp);
static double memreadieee754(unsigned char *buff, int bigendian);
static float memreadieee754f(unsigned char*buff, int bigendian);
static int YcbcrToRGB(int Y, int cb, int cr, unsigned char *red, unsigned char *green, unsigned char *blue);
unsigned char *floadtiff(FILE *fp, int *width, int *height)
{
int enda, endb;
int type;
int magic;
unsigned long offset;
TAG *tags = 0;
int Ntags = 0;
int err;
BASICHEADER header;
unsigned char *answer;
enda = fgetc(fp);
endb = fgetc(fp);
if (enda == 'I' && endb == 'I')
{
type = LITTLE_ENDIAN;
}
else if (endb == 'M' && endb == 'M')
{
type = BIG_ENDIAN;
}
else
goto parse_error;
magic = fget16(type, fp);
if (magic != 42)
goto parse_error;
offset = fget32(type, fp);
fseek(fp, offset, SEEK_SET);
//printf("%c%c %d %ld\n", enda, endb, magic, offset);
tags = floadheader(type, fp, &Ntags);
if (!tags)
goto out_of_memory;
//getchar();
header_defaults(&header);
header.endianness = type;
fillheader(&header, tags, Ntags);
err = header_fixupsections(&header);
if (err)
goto parse_error;
//printf("here %d %d\n", header.imagewidth, header.imageheight);
//printf("Bitspersample%d %d %d\n", header.bitspersample[0], header.bitspersample[1], header.bitspersample[2]);
err = header_not_ok(&header);
if (err)
goto parse_error;
answer = loadraster(&header, fp);
//getchar();
*width = header.imagewidth;
*height = header.imageheight;
freeheader(&header);
killtags(tags, Ntags);
return answer;
parse_error:
freeheader(&header);
killtags(tags, Ntags);
return 0;
out_of_memory:
freeheader(&header);
killtags(tags, Ntags);
return 0;
}
static void header_defaults(BASICHEADER *header)
{
int i;
header->newsubfiletype = 0;
header->imagewidth = -1;
header->imageheight = -1;
header->bitspersample[0] = -1;
header->compression = 1;
header->fillorder = 1;
header->photometricinterpretation =-1;
header->stripoffsets = 0;;
header->Nstripoffsets = 0;
header->samplesperpixel = 0;
header->rowsperstrip = 0;
header->stripbytecounts = 0;
header->Nstripbytecounts = 0;
header->xresolution = 1.0;
header->yresolution = 1.0;
header->resolutionunit = 0;
/* Palette files */
header->colormap = 0;
header->Ncolormap = 0;
/* RGB */
header->planarconfiguration =1;
header->predictor = 1;
/* YCbCr*/
header->LumaRed = 0.299;
header->LumaGreen = 0.587;
header->LumaBlue = 0.114;
//header->YCbCrCoefficients = 0;
header->YCbCrSubSampling_h = 2;
header->YCbCrSubSampling_v = 2;
header->YCbCrPositioning = 1;
header->ReferenceBlackWhite = 0;
/* Class F */
//int BadFaxLines;
//int CleanFaxData;
//int ConsecutiveBadFaxLines;
header->T4options = 0;
header->tilewidth = 0;
header->tileheight = 0;
header->tileoffsets = 0;
header->Ntileoffsets = 0;
header->tilebytecounts = 0;
header->Ntilebytecounts = 0;
for (i = 0; i < 16;i++)
header->sampleformat[i] = 1;
header->smaxsamplevalue = 0;
header->Nsmaxsamplevalue = 0;
header->sminsamplevalue = 0;
header->Nsminsamplevalue = 0;
header->endianness = -1;
}
static void freeheader(BASICHEADER *header)
{
free(header->stripbytecounts);
free(header->stripoffsets);
free(header->tilebytecounts);
free(header->tileoffsets);
free(header->colormap);
free(header->smaxsamplevalue);
free(header->sminsamplevalue);
}
/*
Some TIFF files have tiles in the strip byte counts and so on
fixc this up
*/
static int header_fixupsections(BASICHEADER *header)
{
if (header->tilewidth == 0 && header->tileheight == 0)
{
if (header->Nstripbytecounts > 0 &&
header->Nstripoffsets > 0 &&
header->Nstripbytecounts == header->Nstripoffsets
&& header->Nstripbytecounts < INT_MAX
&& header->Ntileoffsets == 0 &&
header->Ntilebytecounts == 0)
return 0;
else
return -1;
}
else if (header->tilewidth > 0 && header->tileheight > 0)
{
if (header->Ntilebytecounts == 0)
{
header->Ntilebytecounts = header->Nstripbytecounts;
header->Nstripbytecounts = 0;
header->tilebytecounts = header->stripbytecounts;
header->stripbytecounts = 0;
}
if (header->Ntileoffsets == 0)
{
header->Ntileoffsets = header->Nstripoffsets;
header->Nstripoffsets = 0;
header->tileoffsets = header->stripoffsets;
header->stripoffsets = 0;
}
if (header->Ntilebytecounts > 0 &&
header->Ntileoffsets > 0 &&
header->Ntilebytecounts == header->Ntileoffsets
&& header->Ntilebytecounts < INT_MAX
&& header->Nstripoffsets == 0 &&
header->Nstripbytecounts == 0)
return 0;
else
return -1;
}
else
{
return -1;
}
return -1;
}
static int header_not_ok(BASICHEADER *header)
{
int i;
//header->newsubfiletype = 0;
//header->imagewidth = -1;
//header->imageheight = -1;
if (header->imagewidth <= 0 || header->imageheight <= 0
|| (double)header->imagewidth * (double)header->imageheight > LONG_MAX / 4)
goto parse_error;
//header->bitspersample[0] = -1;
if (header->samplesperpixel < 1 || header->samplesperpixel > 16)
goto parse_error;
for (i = 0; i < header->samplesperpixel; i++)
if (header->bitspersample[i] < 1 || (header->bitspersample[i] > 32 && header->bitspersample[i] % 8))
goto parse_error;
//header->compression = 1;
//header->fillorder = 1;
//header->photometricinterpretation = -1;
//header->stripoffsets = 0;;
//header->Nstripoffsets = 0;
//header->samplesperpixel = 0;
//header->rowsperstrip;
//header->stripbytecounts;
//header->Nstripbytecounts;
//header->xresolution = 1.0;
//header->yresolution = 1.0;
//header->resolutionunit = 0;
/* Palette files */
//header->colormap = 0;
//header->Ncolormap = 0;
if (header->Ncolormap < 0 || header->Ncolormap > INT_MAX / 4)
goto parse_error;
/* RGB */
//header->planarconfiguration = 1;
if (header->planarconfiguration != 1 && header->planarconfiguration != 2)
goto parse_error;
/* YCbCr*/
//header->LumaRed = 0.299;
//header->LumaGreen = 0.587;
//header->LumaBlue = 0.114;
if (header->LumaRed <= 0 || header->LumaGreen <= 0 || header->LumaBlue <= 0)
goto parse_error;
//header->YCbCrCoefficients = 0;
//header->YCbCrSubSampling_h = 2;
//header->YCbCrSubSampling_v = 2;
//header->YCbCrPositioning = 1;
//header->ReferenceBlackWhite = 0;
/* Class F */
//int BadFaxLines;
//int CleanFaxData;
//int ConsecutiveBadFaxLines;
//header->tilewidth = 0;
//header->tileheight = 0;
if (header->tilewidth < 0 || header->tileheight < 0)
goto parse_error;
if ((double)header->tilewidth * (double)header->tileheight > LONG_MAX / 4)
goto parse_error;
//header->tileoffsets = 0;
//header->Ntileoffsets = 0;
//header->tilebytecounts = 0;
//header->Ntilebytecounts = 0;
//header->sampleformat = 1;
//header->smaxsamplevalue = 0;
//header->Nsmaxsamplevalue = 0;
//header->sminsamplevalue = 0;
//header->Nsminsamplevalue = 0;
//header->endianness = -1;
return 0;
parse_error:
return -1;
}
static int fillheader(BASICHEADER *header, TAG *tags, int Ntags)
{
int i;
unsigned long ii;
int jj;
for (i = 0; i < Ntags; i++)
{
if (tags[i].bad)
continue;
switch (tags[i].tagid)
{
case TID_IMAGEWIDTH:
header->imagewidth = (int)tags[i].scalar;
break;
case TID_IMAGEHEIGHT:
header->imageheight = (int)tags[i].scalar;
break;
case TID_BITSPERSAMPLE:
if (tags[i].datacount > 16)
goto parse_error;
for (ii = 0; ii < tags[i].datacount; ii++)
header->bitspersample[ii] = (int) tag_getentry(&tags[i], ii);
break;
case TID_COMPRESSION:
header->compression = (int) tags[i].scalar;
break;
case TID_PHOTOMETRICINTERPRETATION:
header->photometricinterpretation = (int)tags[i].scalar;
break;
case TID_FILLORDER:
header->fillorder = (int)tags[i].scalar;
break;
case TID_STRIPOFFSETS:
header->stripoffsets = malloc(tags[i].datacount * sizeof(unsigned long));
if (!header->stripoffsets)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->stripoffsets[ii] = (int) tag_getentry(&tags[i], ii);
header->Nstripoffsets = tags[i].datacount;
break;
case TID_SAMPLESPERPIXEL:
header->samplesperpixel = (int)tags[i].scalar;
break;
case TID_ROWSPERSTRIP:
header->rowsperstrip = (int)tags[i].scalar;
break;
case TID_STRIPBYTECOUNTS:
header->stripbytecounts = malloc(tags[i].datacount * sizeof(unsigned long));
if (!header->stripbytecounts)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->stripbytecounts[ii] = (unsigned long)tag_getentry(&tags[i], ii);
header->Nstripbytecounts = tags[i].datacount;
break;
case TID_PLANARCONFIGUATION:
header->planarconfiguration = (int) tags[i].scalar;
break;
case TID_T4OPTIONS:
header->T4options = (unsigned long)tags[i].scalar;
break;
case TID_PREDICTOR:
header->predictor = (int)tags[i].scalar;
break;
case TID_COLORMAP:
if (tags[i].datacount > INT_MAX / 3)
goto parse_error;
header->Ncolormap = tags[i].datacount / 3;
header->colormap = malloc(header->Ncolormap * 3);
if (!header->colormap)
goto out_of_memory;
for (jj = 0; jj < header->Ncolormap; jj++)
header->colormap[jj *3] = (int)tag_getentry(&tags[i], jj) / 256;
for (jj = 0; jj < header->Ncolormap; jj++)
header->colormap[jj * 3+1] = (int)tag_getentry(&tags[i], jj + header->Ncolormap) / 256;
for (jj = 0; jj < header->Ncolormap; jj++)
header->colormap[jj * 3 + 2] = (int)tag_getentry(&tags[i], jj + header->Ncolormap*2) / 256;
break;
case TID_TILEWIDTH:
header->tilewidth = (int) tags[i].scalar;
break;
case TID_TILELENGTH:
header->tileheight = (int)tags[i].scalar;
break;
case TID_TILEOFFSETS:
header->tileoffsets = malloc(tags[i].datacount * sizeof(unsigned long));
if (!header->tileoffsets)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->tileoffsets[ii] = (unsigned long)tag_getentry(&tags[i], ii);
header->Ntileoffsets = tags[i].datacount;
break;
case TID_TILEBYTECOUNTS:
header->tilebytecounts = malloc(tags[i].datacount * sizeof(unsigned long));
if (!header->tilebytecounts)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->tilebytecounts[ii] = (unsigned long)tag_getentry(&tags[i], ii);
header->Ntilebytecounts = tags[i].datacount;
break;
case TID_SAMPLEFORMAT:
if (tags[i].datacount != header->samplesperpixel)
goto parse_error;
for (ii = 0; ii < tags[i].datacount;ii++)
header->sampleformat[ii] = (int)tag_getentry(&tags[i], ii);
break;
case TID_SMINSAMPLEVALUE:
header->sminsamplevalue = malloc(tags[i].datacount * sizeof(double));
if (!header->sminsamplevalue)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->sminsamplevalue[ii] = (unsigned long)tag_getentry(&tags[i], ii);
header->Nsminsamplevalue = tags[i].datacount;
break;
case TID_SMAXSAMPLEVALUE:
header->smaxsamplevalue = malloc(tags[i].datacount * sizeof(double));
if (!header->smaxsamplevalue)
goto out_of_memory;
for (ii = 0; ii < tags[i].datacount; ii++)
header->smaxsamplevalue[ii] = (unsigned long)tag_getentry(&tags[i], ii);
header->Nsmaxsamplevalue = tags[i].datacount;
break;
case TID_YCBCRCOEFFICIENTS:
if (tags[i].datacount < 3)
goto parse_error;
header->LumaRed = tag_getentry(&tags[i], 0);
header->LumaGreen = tag_getentry(&tags[i], 1);
header->LumaBlue = tag_getentry(&tags[i], 2);
break;
case TID_YCBCRSUBSAMPLING:
if (tags[i].datacount < 2)
goto parse_error;
header->YCbCrSubSampling_h = (int)tag_getentry(&tags[i], 0);
header->YCbCrSubSampling_v = (int)tag_getentry(&tags[i], 1);
break;
case TID_YCBCRPOSITIONING:
header->YCbCrPositioning = (int)tags[i].scalar;
break;
}
}
return 0;
out_of_memory:
return -1;
parse_error:
return -2;
}
static unsigned char *loadraster(BASICHEADER *header, FILE *fp)
{
unsigned char *rgba = 0;
unsigned char *strip = 0;
int i;
unsigned long ii;
int row = 0;
int swidth, sheight;
int tilesacross = 0;
int sample_index;
rgba = malloc(header->imagewidth * header->imageheight * 4);
if (!rgba)
goto out_of_memory;
if (header->tilewidth)
tilesacross = (header->imagewidth + header->tilewidth - 1) / header->tilewidth;
if (header->planarconfiguration == 2)
{
if (header->photometricinterpretation == PI_RGB)
{
sample_index = 0;
for (i = 0; i < header->Nstripoffsets; i++)
{
if (sample_index > 4)
continue;
strip = readchannel(header, i, &swidth, &sheight, fp);
if (!strip)
goto out_of_memory;
for (ii = 0; ii < (unsigned long) (swidth * sheight); ii++)
{
rgba[((row + ii / swidth)*header->imagewidth + (ii%swidth)) * 4 + sample_index] = strip[ii];
}
row += sheight;
if (row >= header->imageheight)
{
row = 0;
sample_index++;
}
free(strip);
}
return rgba;
}
if (header->photometricinterpretation == PI_CMYK)
{
sample_index = 0;
for (i = 0; i < header->Nstripoffsets; i++)
{
if (sample_index > 4)
continue;
strip = readchannel(header, i, &swidth, &sheight, fp);
if (!strip)
goto out_of_memory;
for (ii = 0; ii < (unsigned long)(swidth * sheight); ii++)
{
rgba[((row + ii / swidth)*header->imagewidth + (ii%swidth)) * 4 + sample_index] = strip[ii];
}
row += sheight;
if (row >= header->imageheight)
{
row = 0;
sample_index++;
}
free(strip);
}
for (i = 0; i < header->imagewidth * header->imageheight; i++)
{
int red, green, blue;
red = ((255 - rgba[i*4])*(255 - rgba[i*4+3])) / 255;
green = ((255 - rgba[i*4+1])*(255 - rgba[i*4+3])) / 255;
blue = ((255 - rgba[i*4+2])*(255 - rgba[i*4+3])) / 255;
rgba[i*4] = red;
rgba[i*4+1] = green;
rgba[i*4+2] = blue;
rgba[i*4+3] = 255;
}
return rgba;
}
}
if (header->Nstripoffsets > 0 && tilesacross == 0)
{
for (i = 0; i < header->Nstripoffsets; i++)
{
strip = readstrip(header, i, &swidth, &sheight, fp);
if (!strip)
goto out_of_memory;
memcpy(rgba + row * header->imagewidth * 4, strip, swidth *sheight * 4);
row += sheight;
free(strip);
}
}
if (header->Nstripoffsets > 0 && tilesacross != 0)
{
header->Ntileoffsets = header->Nstripoffsets;
header->tileoffsets = header->stripoffsets;
header->stripoffsets = 0;
header->Nstripoffsets = 0;
}
if (header->Ntilebytecounts == 0 && tilesacross != 0)
{
header->Ntilebytecounts = header->Nstripoffsets;
header->tilebytecounts = header->stripbytecounts;
header->stripbytecounts = 0;
header->Nstripbytecounts = 0;
}
if (header->Ntileoffsets > 0)
{
for (i = 0; i < header->Ntileoffsets; i++)
{
strip = readtile(header, i, &swidth, &sheight, fp);
if (!strip)
goto out_of_memory;
rgbapaste(rgba, header->imagewidth, header->imageheight, strip, swidth, sheight,
(i % tilesacross) * header->tilewidth, (i / tilesacross) * header->tileheight);
free(strip);
}
}
return rgba;
out_of_memory:
free(rgba);
free(strip);
return 0;
}
/*//////////////////////////////////////////////////////////////////////////////////////////////////*/
/* stip tile and plane loading section*/
/*//////////////////////////////////////////////////////////////////////////////////////////////////*/
static int planetochannel(unsigned char *out, int width, int height, unsigned char *bits, unsigned long Nbytes, BASICHEADER *header, int sample_index);