-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiff.c
3163 lines (2588 loc) · 98.5 KB
/
tiff.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.c */
/* The TIFF writer contained in this code */
/* assumed that the machine on which it */
/* is running stores numbers with the */
/* most-significant byte first, i.e., in */
/* the "big-endian" byte-order. */
#include "tiff.h"
struct Rational {
unsigned int Numer;
unsigned int Denom;
};
union TIFF_value { /* don't know the type of the */
unsigned char UChar; /* data value(s) in advance */
unsigned short UShort;
unsigned int ULong;
struct Rational Rat;
unsigned char *UCharArray;
unsigned short *UShortArray;
unsigned int *ULongArray;
struct Rational *RatArray;
};
struct TIFF_field {
unsigned short Tag; /* tag for TIFF field */
unsigned short Type; /* type for values in field */
unsigned int Count; /* number of values in field */
union TIFF_value Value; /* the ValueOrOffset word in the */
/* field contains the Value */
/* instead of pointing to the */
/* Value if and only if the Value */
/* fits into 4 bytes; in this */
/* case the value is stored in */
/* the lower-numbered bytes; */
/* if value does not fit into 4 */
/* bytes, the entry there is the */
/* offset (wrt beginning of file) */
/* to the value */
int SizeOfType; /* number of bytes per element of */
/* the Value */
};
struct IFD {
unsigned short NumberOfFields; /* number of IFD entries for this */
/* image */
struct TIFF_field *Fields; /* this array should have */
/* NumberOfFields elements */
};
struct TIFF_header {
unsigned short ByteOrder; /* for interpreting multi-byte */
/* numbers */
/* 0x4949 = little-endian */
/* 0x4D4D = big-endian */
unsigned short FortyTwo; /* the second word in a TIFF */
/* image header is always 42 */
unsigned int OffsetOfFirstIFD; /* offset of first Image File */
/* Directory (IFD) (wrt beginning */
/* of file; i.e., in bytes, */
/* counting the first byte of */
/* the file as number 0) */
};
struct DataLocation {
unsigned int StripsPerImage;
unsigned int rows_per_strip;
unsigned int *strip_offsets;
unsigned int *strip_byte_counts;
unsigned int offset_of_byte_after_data;
unsigned int bytes_per_row;
};
/* subroutines */
static int FreeIFD ( struct IFD *ifd );
static int FreeFieldValues ( struct TIFF_field *field );
static int WriteHeaderAndIFD ( FILE *fp, struct IFD *ifd,
struct TIFF_header *header );
static int WriteIFD ( FILE *fp, struct IFD *ifd,
struct TIFF_header *header );
static int WriteField ( FILE *fp, struct TIFF_field *field,
unsigned int starting_offset,
unsigned int *outside_IFD_offset );
static int WriteValue ( FILE *fp, struct TIFF_field *field,
unsigned int *outside_IFD_offset );
static int WriteArrayOfValues ( FILE *fp, struct TIFF_field *field );
static int WriteSingleValue ( FILE *fp, struct TIFF_field *field );
static int WriteHeader ( FILE *fp, struct TIFF_header *header );
static int WriteRational ( FILE *fp, struct Rational *Rat );
static int WriteUnsignedLong ( FILE *fp, unsigned int *UnsignedLong );
static int WriteUnsignedShort ( FILE *fp, unsigned short *UnsignedShort );
static int WriteUnsignedChar ( FILE *fp, unsigned char *UnsignedChar );
static int WriteImageData ( FILE *fp, struct TIFF_img *img,
struct DataLocation *DataLoc );
static void FreeStripBuffer ( unsigned char *buffer );
static void AllocateStripBuffer ( unsigned char **buffer,
struct DataLocation *DataLoc,
int width );
static int PutStrip ( FILE *fp, struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned char *strip_buf,
unsigned int strip_index );
static int PackStrip ( struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned char *buffer,
unsigned int strip_index );
static int WriteStrip ( FILE *fp, struct DataLocation *DataLoc,
unsigned char *strip_buf,
unsigned int strip_index );
static int MakeImageDataLocInfo ( struct TIFF_img *img,
struct DataLocation *DataLoc );
static int ComputeStripsPerImage ( int height, struct DataLocation *DataLoc );
static int DetermineRowsPerStrip ( struct TIFF_img *img,
struct DataLocation *DataLoc );
static int DetermineBytesPerRow ( struct TIFF_img *img,
struct DataLocation *DataLoc );
static int PrepareHeaderAndIFD ( struct TIFF_img *img, struct IFD *ifd,
struct TIFF_header *header,
struct DataLocation *DataLoc );
static int PrepareHeader ( struct TIFF_header *header,
struct DataLocation *DataLoc );
static int PrepareIFD ( struct TIFF_img *img,
struct IFD *ifd,
struct DataLocation *DataLoc );
static int AddSpecialFieldEntries ( struct TIFF_img *img,
struct IFD *ifd );
static int AddGrayscaleFields ( struct IFD *ifd, char TIFF_type );
static int AddPaletteColorFields ( struct TIFF_img *img,
struct IFD *ifd );
static int AddColorFields ( struct IFD *ifd, char TIFF_type );
static int SortFields ( struct IFD *ifd );
static void SwitchFields ( struct TIFF_field *FieldOne,
struct TIFF_field *FieldTwo );
static int AddCoreFieldEntries ( struct TIFF_img *img,
struct IFD *ifd,
struct DataLocation *DataLoc );
static int MakeSamplesPerPixelField ( struct IFD *ifd );
static int MakeBitsPerSampleField ( struct IFD *ifd, char TIFF_type );
static int MakeResolutionUnitField ( struct IFD *ifd );
static int MakeResolutionField ( struct IFD *ifd, unsigned short Tag );
static int MakeStripByteCountsField ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int MakeRowsPerStripField ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int MakeStripOffsetsField ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int MakePhotometricInterpretationField ( struct IFD *ifd, char TIFF_type );
static int MakeCompressionField ( struct IFD *ifd, char compress_type );
static int MakeImageWidthOrLengthField ( struct IFD *ifd,
unsigned short Tag,
int dimension );
static int MakeDefaultRowsPerStripField ( struct IFD *ifd );
static int MakeColorMapField ( struct TIFF_img *img,
struct IFD *ifd );
static unsigned int GetMaxValUL ( unsigned int *array,
unsigned int n_elements );
static int GetImageData ( FILE *fp, struct TIFF_img *img, struct IFD *ifd );
static int ReadImageData ( FILE *fp, struct TIFF_img *img, struct IFD *ifd );
static int PutColorMapValuesIntoTable ( struct TIFF_img *img,
struct IFD *ifd );
static void FreeDataLocation ( struct DataLocation *DataLoc );
static int GetStrip ( FILE *fp, struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned char *strip_buf,
unsigned int strip_index );
static int UnpackStrip ( struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned int strip_index,
unsigned char *buffer );
static int ReadStrip ( FILE *fp, struct DataLocation *DataLoc,
unsigned int strip_index,
unsigned char *strip_buf );
static int GetUShortValueFromField ( struct IFD *ifd, unsigned short Tag,
unsigned short *Value );
static int GetImageDataLocInfo ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int GetStripOffsets ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int GetStripByteCounts ( struct IFD *ifd,
struct DataLocation *DataLoc );
static int PutValuesIntoULongArray ( struct TIFF_field *field,
unsigned int **array,
unsigned int num_elements );
static int GetNumberOfStrips ( struct IFD *ifd,
unsigned int *StripsPerImage );
static int GetRowsPerStrip ( struct IFD *ifd,
unsigned int *rows_per_strip );
static void WrongValueType ( char *FunctionName, unsigned short Tag,
unsigned short Type );
static int AllocateImageDataArray ( struct TIFF_img *img, struct IFD *ifd );
static int AllocateColorMap ( unsigned char ***cmap, struct IFD *ifd );
static int GetHeightAndWidth ( struct IFD *ifd, int *height, int *width );
static int GetCompression ( struct IFD *ifd, char *compress_type );
static int VerifyIFD ( struct IFD *ifd, char *TIFF_type );
static int GetImageType ( struct IFD *ifd, char *TIFF_type );
static int IsImageFullColor ( struct IFD *ifd );
static int IsImagePaletteColor ( struct IFD *ifd );
static int IsImageGrayscale ( struct IFD *ifd );
static struct TIFF_field *GetFieldStructure ( struct IFD *ifd,
unsigned short Tag );
static int CheckForCoreFields ( struct IFD *ifd );
static int WhatAboutCoreField ( struct IFD *ifd, unsigned short Tag );
static int AddDefaultField ( struct IFD *ifd, unsigned short Tag );
static void TellUserACoreFieldIsNecessary ( unsigned short Tag );
static int IsThereADefaultFor ( unsigned short Tag );
static int IsThereAFieldFor ( struct IFD *ifd, unsigned short Tag );
static int ReadIFD ( FILE *fp, struct IFD *ifd,
struct TIFF_header *header, char *TIFF_type );
static int SeeIfThereAreOtherIFDs ( FILE *fp, unsigned int OffsetOfIFD,
unsigned short MaxNumberOfFields );
static int SetFilePositionAtFirstByteOfIthField ( FILE *fp,
unsigned int OffsetOfIFD,
unsigned short i );
static int CopyFieldIfTagIsRecognized ( FILE *fp, struct IFD *ifd );
static int AllocateAndCopyField ( FILE *fp, struct IFD *ifd,
unsigned short Tag, unsigned short Type );
static int IsTypeExpectedWithTag ( unsigned short Tag, unsigned short Type );
static int IsTypeRecognized ( unsigned short Type );
static int CopyField ( FILE *fp, struct TIFF_field *field,
unsigned short Tag, unsigned short Type );
static int GetSizeOfType ( struct TIFF_field *field );
static int CopyValue ( FILE *fp, struct TIFF_field *field );
static int GetArrayOfValues ( FILE *fp, struct TIFF_field *field );
static int GetSingleValue ( FILE *fp, struct TIFF_field *field );
static int AllocateArrayOfValues ( struct TIFF_field *field );
static int CopyCount ( FILE *fp, struct TIFF_field *field );
static int AllocateNewField ( struct IFD *ifd );
static int IsTagRecognized ( unsigned short TempTag );
static int GetRational ( FILE *fp, struct Rational *Rat );
static int GetUnsignedLong ( FILE *fp, unsigned int *UnsignedLong );
static int GetUnsignedShort ( FILE *fp, unsigned short *UnsignedShort );
static int GetUnsignedChar ( FILE *fp, unsigned char *UnsignedChar );
static int GetNumberOfFieldsInInput ( FILE *fp, unsigned int OffsetOfIFD,
unsigned short *NumFieldsInInputFile );
static int SetFilePosition ( FILE *fp, unsigned int position );
static int ReadHeader ( FILE *fp, struct TIFF_header *header );
static int CheckTypeSizes ( void );
static int GetByteOrder ( FILE *fp, struct TIFF_header *header );
static int CheckFortyTwo ( FILE *fp, struct TIFF_header *header );
static int GetOffsetOfFirstIFD ( FILE *fp, struct TIFF_header *header );
/* static void PrintIFD ( struct IFD *ifd );*/
/* static void PrintField ( struct TIFF_field *field );*/
static void *mget_spc(int num,size_t size);
static void **get_img(int wd,int ht,size_t size);
static void free_img(void **pt);
/* TIFF field tags */
#define ImageWidth 256 /* width */
#define ImageLength 257 /* length (height) */
#define BitsPerSample 258 /* for image data, number */
/* of bits per component */
/* (4 and 8 are the two */
/* allowable values for */
/* grayscale and */
/* palette-color) */
/* (for full-color RGB, */
/* 8 bits per component; */
/* so 8,8,8) */
#define Compression 259 /* data can be stored */
#define NoCompression 1 /* uncompressed or */
#define PackBits 32773 /* compressed */
#define OneDimModifiedHuffman 2 /* (1DModHuff compression */
/* is not available for */
/* grayscale, palette- */
/* color, or full RGB */
/* color) */
#define PhotometricInterpretation 262 /* differentiates between */
#define WhiteIsZero 0 /* white-on-black or */
#define BlackIsZero 1 /* black-on-white for */
/* binary images, */
#define RGB 2 /* full-color RGB, and */
#define PaletteColor 3 /* palette-color */
#define StripOffsets 273 /* for each strip, the */
/* byte offset for that */
/* strip (wrt beginning */
/* of file) */
#define SamplesPerPixel 277 /* number of components */
/* per pixel; for full- */
/* color RGB this must be */
/* at least 3 */
#define RowsPerStrip 278 /* number of rows in each */
/* strip (except possibly */
/* the last strip) */
#define StripByteCounts 279 /* for each strip, the */
/* number of bytes in */
/* that strip, after any */
/* compression */
#define XResolution 282 /* number of pixels per */
/* ResolutionUnit (see */
/* above) in ImageWidth */
/* direction */
#define YResolution 283 /* number of pixels per */
/* ResolutionUnit (see */
/* above) in ImageLength */
/* direction */
#define ResolutionUnit 296 /* absolute unit of meas. */
#define NoAbsoluteUnit 1
#define Inch 2 /* (this is the default) */
#define Centimeter 3
#define ColorMap 320 /* color map for palette- */
/* color images */
/* (the count for this */
/* field has to be */
/* 3*(2^BitsPerSample)) */
/* TIFF field types */
#define BYTE 1 /* 1-byte unsigned int */
#define SHORT 3 /* 2-byte unsigned int */
#define LONG 4 /* 4-byte unsigned int */
#define RATIONAL 5 /* two LONGS: first is */
/* numerator, second is */
/* denominator */
/* TIFF byte-order possibilities */
#define LittleEndian 0x4949
#define BigEndian 0x4d4d
/* other useful defs */
#define ERROR 1
#define NO_ERROR 0
#define NO 0
#define YES 1
#define HostByteOrder ((charsequence[0] == 1) ? BigEndian : LittleEndian)
#define LongReverse(x) \
((unsigned int)((((unsigned int)(x) & 0x000000ffU) << 24) | \
(((unsigned int)(x) & 0x0000ff00U) << 8) | \
(((unsigned int)(x) & 0x00ff0000U) >> 8) | \
(((unsigned int)(x) & 0xff000000U) >> 24)))
#define ShortReverse(x) \
((unsigned short int)((((unsigned short int)(x) & 0x00ffU) << 8) | \
(((unsigned short int)(x) & 0xff00U) >> 8)))
unsigned int longsequence[1] = {0x01020304u};
unsigned char *charsequence = (unsigned char *) longsequence;
unsigned short FileByteOrder = BigEndian;
int write_TIFF ( FILE *fp, struct TIFF_img *img )
{
struct IFD ifd;
struct TIFF_header header;
struct DataLocation DataLoc;
/* ensure that each type will be stored */
/* in the expected number of bytes */
if ( CheckTypeSizes ( ) == ERROR ) return ( ERROR );
/* write image data, store information about */
/* location of the data in DataLoc structure */
if ( WriteImageData ( fp, img, &(DataLoc) ) == ERROR )
return ( ERROR );
/* prepare header and image file directory */
if ( PrepareHeaderAndIFD ( img, &(ifd), &(header), &(DataLoc) )
== ERROR ) return ( ERROR );
/* write header and image file directory */
if ( WriteHeaderAndIFD ( fp, &(ifd), &(header) ) == ERROR )
return ( ERROR );
/* free arrays in DataLocation and IFD structures */
FreeDataLocation ( &(DataLoc) );
if ( FreeIFD ( &(ifd) ) == ERROR ) return ( ERROR );
return ( NO_ERROR );
}
int get_TIFF ( struct TIFF_img *img, int height, int width , char TIFF_type )
{
int i;
/* set height, width, TIFF_type, and compress_type */
img->height = height;
img->width = width;
img->TIFF_type = TIFF_type;
img->compress_type = 'u';
/* check image height/width */
if ( ( img->height <= 0 ) || ( img->width <= 0 ) ) {
fprintf ( stderr, "tiff.c: function get_TIFF:\n" );
fprintf ( stderr, "image height and width must be positive\n" );
return ( ERROR );
}
/* if image is grayscale */
if ( img->TIFF_type == 'g' ) {
img->mono = ( unsigned char ** )
get_img ( img->width, img->height, sizeof ( unsigned char ) );
return ( NO_ERROR );
}
/* if image is palette-color */
if ( img->TIFF_type == 'p' ) {
img->mono = ( unsigned char ** )
get_img ( img->width, img->height, sizeof ( unsigned char ) );
img->cmap = ( unsigned char ** ) get_img ( 3, 256, sizeof ( unsigned char ) );
return ( NO_ERROR );
}
/* if image is full color */
if ( img->TIFF_type == 'c' ) {
img->color = ( unsigned char *** )
mget_spc ( 3, sizeof ( unsigned char ** ) );
for ( i = 0; i < 3; i++ )
img->color[i] = ( unsigned char ** )
get_img ( img->width, img->height,
sizeof ( unsigned char ) );
return ( NO_ERROR );
}
fprintf ( stderr, "tiff.c: function get_TIFF:\n" );
fprintf ( stderr, "allocation for image of type %c is not supported",
img->TIFF_type );
return ( ERROR );
}
void free_TIFF ( struct TIFF_img *img )
{
int i;
/* grayscale */
if ( img->TIFF_type == 'g' ) {
free_img ( ( void ** ) ( img->mono ) );
}
/* palette-color */
if ( img->TIFF_type == 'p' ) {
free_img ( ( void ** ) ( img->mono ) );
free_img ( ( void ** ) ( img->cmap ) );
}
/* full color */
if ( img->TIFF_type == 'c' ) {
for ( i = 0; i < 3; i++ ) free_img ( ( void ** ) ( img->color[i] ) );
}
}
static int FreeIFD ( struct IFD *ifd )
{
unsigned int i;
/* free array(s) of values */
for ( i = 0; i < ifd->NumberOfFields; i++ )
if ( ifd->Fields[i].Count > 1 )
if ( FreeFieldValues ( &(ifd->Fields[i]) ) == ERROR )
return ( ERROR );
/* free IFD fields */
free ( ( void * ) ifd->Fields );
return ( NO_ERROR );
}
static int FreeFieldValues ( struct TIFF_field *field )
{
if ( field->Type == BYTE ) {
free ( ( void * ) field->Value.UCharArray );
return ( NO_ERROR );
}
if ( field->Type == SHORT ) {
free ( ( void * ) field->Value.UShortArray );
return ( NO_ERROR );
}
if ( field->Type == LONG ) {
free ( ( void * ) field->Value.ULongArray );
return ( NO_ERROR );
}
if ( field->Type == RATIONAL ) {
free ( ( void * ) field->Value.RatArray );
return ( NO_ERROR );
}
fprintf ( stderr, "tiff.c: function FreeFieldValues:\n" );
fprintf ( stderr, "not prepared to free value array of type %d",
field->Type );
return ( ERROR );
}
static int WriteHeaderAndIFD ( FILE *fp, struct IFD *ifd,
struct TIFF_header *header )
{
/* write header */
if ( WriteHeader ( fp, header ) == ERROR ) return ( ERROR );
/* write IFD */
if ( WriteIFD ( fp, ifd, header ) == ERROR ) return ( ERROR );
return ( NO_ERROR );
}
static int WriteIFD ( FILE *fp, struct IFD *ifd,
struct TIFF_header *header )
{
unsigned int outside_IFD_offset, i;
unsigned int starting_offset, zero;
/* compute location of first byte after IFD and four */
/* bytes of zeros; this number will be maintained as */
/* the address at which field values (which don't fit */
/* into four bytes) should be written */
outside_IFD_offset = header->OffsetOfFirstIFD + 2 +
( unsigned int )
( ifd->NumberOfFields * 12 ) + 4;
/* set file position */
if ( SetFilePosition ( fp, header->OffsetOfFirstIFD )
== ERROR ) {
fprintf ( stderr, "tiff.c: function WriteIFD:\n" );
fprintf ( stderr, "error setting file position\n" );
return ( ERROR );
}
/* write number of fields */
if ( WriteUnsignedShort ( fp, &(ifd->NumberOfFields) )
== ERROR ) return ( ERROR );
/* loop through fields */
for ( i = 0; i < ifd->NumberOfFields; i++ ) {
/* compute starting offset for field */
starting_offset = header->OffsetOfFirstIFD + 2 + (12 * i);
/* write field */
if ( WriteField ( fp, &(ifd->Fields[i]), starting_offset,
&(outside_IFD_offset) ) == ERROR )
return ( ERROR );
}
/* write four bytes of zero to signify that this */
/* is that last (only) IFD to have been written */
zero = 0;
if ( WriteUnsignedLong ( fp, &(zero) ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
static int WriteField ( FILE *fp, struct TIFF_field *field,
unsigned int starting_offset,
unsigned int *outside_IFD_offset )
{
/* set file position at starting_offset */
if ( SetFilePosition ( fp, starting_offset ) == ERROR )
return ( ERROR );
/* write tag */
if ( WriteUnsignedShort ( fp, &(field->Tag) ) == ERROR )
return ( ERROR );
/* write type */
if ( WriteUnsignedShort ( fp, &(field->Type) ) == ERROR )
return ( ERROR );
/* write Count */
if ( WriteUnsignedLong ( fp, &(field->Count) ) == ERROR )
return ( ERROR );
/* write value if value fits into 4 */
/* bytes; else write offset to value, */
/* and go to the offset to write down */
/* the actual field value(s) */
if ( WriteValue ( fp, field, outside_IFD_offset ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
static int WriteValue ( FILE *fp, struct TIFF_field *field,
unsigned int *outside_IFD_offset )
{
/* get size of type */
if ( GetSizeOfType ( field ) == ERROR ) return ( ERROR );
/* if value will not fit inside four bytes */
if ( 4 < field->Count * field->SizeOfType ) {
/* write offset to place where value(s) will be written */
if ( WriteUnsignedLong ( fp, outside_IFD_offset )
== ERROR ) return ( ERROR );
/* set file position where value(s) should be written */
if ( SetFilePosition ( fp, *outside_IFD_offset ) == ERROR ) {
fprintf ( stderr, "tiff.c: function WriteValue:\n" );
fprintf ( stderr, "error setting file position\n" );
return ( ERROR );
}
/* update *outside_IFD_offset; fix it so that the next */
/* value written outside the ValueOrOffset (the 4-byte */
/* field entry in IFD) will begin on a word boundary */
*outside_IFD_offset += field->Count * field->SizeOfType;
if ( ( *outside_IFD_offset % 2 ) != 0 )
*outside_IFD_offset = *outside_IFD_offset + 1;
}
/* either there is only 1 value or there are multiple values */
if ( field->Count == 1 ) {
if ( WriteSingleValue ( fp, field ) == ERROR ) {
fprintf ( stderr, "cound not write data value ");
fprintf ( stderr, "for tag number %d\n", field->Tag);
return ( ERROR );
}
}
else if ( field->Count > 1 ) {
if ( WriteArrayOfValues ( fp, field ) == ERROR ) {
fprintf ( stderr, "cound not write data values ");
fprintf ( stderr, "for tag number %d\n", field->Tag);
return ( ERROR );
}
}
else {
fprintf ( stderr, "tiff.c: function WriteValue:\ncount for " );
fprintf ( stderr, "field with tag %d is zero\n", field->Tag );
return ( ERROR );
}
return ( NO_ERROR );
}
static int WriteArrayOfValues ( FILE *fp, struct TIFF_field *field )
{
unsigned int i;
if ( field->Type == BYTE ) {
for ( i = 0; i < field->Count; i++ )
if ( WriteUnsignedChar ( fp, &(field->Value.UCharArray[i]) )
== ERROR ) return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == SHORT ) {
for ( i = 0; i < field->Count; i++ )
if ( WriteUnsignedShort ( fp, &(field->Value.UShortArray[i]) )
== ERROR ) return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == LONG ) {
for ( i = 0; i < field->Count; i++ )
if ( WriteUnsignedLong ( fp, &(field->Value.ULongArray[i]) )
== ERROR ) return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == RATIONAL ) {
for ( i = 0; i < field->Count; i++ )
if ( WriteRational ( fp, &(field->Value.RatArray[i]) )
== ERROR ) return ( ERROR );
return ( NO_ERROR );
}
fprintf ( stderr, "tiff.c: function WriteArrayOfValues:\n" );
fprintf ( stderr, "writer not prepared to write values of\n" );
fprintf ( stderr, "type %d\n", field->Type );
return ( ERROR );
}
static int WriteSingleValue ( FILE *fp, struct TIFF_field *field )
{
if ( field->Type == BYTE ) {
if ( WriteUnsignedChar ( fp, &(field->Value.UChar) ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == SHORT ) {
if ( WriteUnsignedShort ( fp, &(field->Value.UShort) ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == LONG ) {
if ( WriteUnsignedLong ( fp, &(field->Value.ULong) ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
else if ( field->Type == RATIONAL ) {
if ( WriteRational ( fp, &(field->Value.Rat) ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
fprintf ( stderr, "tiff.c: function WriteSingleValue:\n" );
fprintf ( stderr, "writer not prepared to write value of\n" );
fprintf ( stderr, "type %d\n", field->Type );
return ( ERROR );
}
static int WriteHeader ( FILE *fp, struct TIFF_header *header )
{
/* set file position at beginning of file */
if ( SetFilePosition ( fp, 0 ) == ERROR ) return ( ERROR );
/* write byte-order word */
if ( WriteUnsignedShort ( fp, &(header->ByteOrder) ) == ERROR )
return ( ERROR );
/* write forty-two */
if ( WriteUnsignedShort ( fp, &(header->FortyTwo) ) == ERROR )
return ( ERROR );
/* write offset of first IFD */
if ( WriteUnsignedLong ( fp, &(header->OffsetOfFirstIFD) )
== ERROR )
return ( ERROR );
return ( NO_ERROR );
}
static int WriteRational ( FILE *fp, struct Rational *Rat )
{
unsigned int numerator, denumerator;
if (HostByteOrder == BigEndian) {
numerator = Rat->Numer;
denumerator = Rat->Denom;
} else {
numerator = LongReverse(Rat->Numer);
denumerator = LongReverse(Rat->Denom);
}
if ( ( 1 != fwrite ( &numerator,
sizeof ( unsigned int ), 1, fp ) ) ||
( 1 != fwrite ( &denumerator,
sizeof ( unsigned int ), 1, fp ) ) ) {
fprintf ( stderr,"error writing rational number\n" );
return ( ERROR );
}
return ( NO_ERROR );
}
static int WriteUnsignedLong ( FILE *fp, unsigned int *UnsignedLong )
{
unsigned int unsignedlong;
if (HostByteOrder == BigEndian)
unsignedlong = *UnsignedLong;
else
unsignedlong = LongReverse(*UnsignedLong);
if ( 1 != fwrite ( ( unsigned int * ) &unsignedlong,
sizeof ( unsigned int ), 1, fp ) ) {
fprintf ( stderr,"error writing unsigned int\n" );
return ( ERROR );
}
return ( NO_ERROR );
}
static int WriteUnsignedShort ( FILE *fp, unsigned short *UnsignedShort )
{
unsigned short unsignedshort;
if (HostByteOrder == BigEndian)
unsignedshort = *UnsignedShort;
else
unsignedshort = ShortReverse(*UnsignedShort);
if ( 1 != fwrite ( ( unsigned short * ) &unsignedshort,
sizeof ( unsigned short ), 1, fp ) ) {
fprintf ( stderr,"error writing unsigned short\n" );
return ( ERROR );
}
return ( NO_ERROR );
}
static int WriteUnsignedChar ( FILE *fp, unsigned char *UnsignedChar )
{
if ( 1 != fwrite ( ( unsigned char * ) UnsignedChar,
sizeof ( unsigned char ), 1, fp ) ) {
fprintf ( stderr,"error writing unsigned char\n" );
return ( ERROR );
}
return ( NO_ERROR );
}
static int WriteImageData ( FILE *fp, struct TIFF_img *img,
struct DataLocation *DataLoc )
{
unsigned char *strip_buf;
unsigned int strip_index;
/* precompute rows per strip, bytes per row, and the number */
/* of strips; allocate space for the arrays (strip_offsets and */
/* strip_byte_count) in the DataLocation structure */
if ( MakeImageDataLocInfo ( img, DataLoc ) == ERROR )
return ( ERROR );
AllocateStripBuffer ( &(strip_buf), DataLoc, img->width );
/* write down one strip at a time */
for ( strip_index = 0; strip_index < DataLoc->StripsPerImage; strip_index++ )
if ( PutStrip ( fp, img, DataLoc, strip_buf, strip_index ) == ERROR )
return ( ERROR );
FreeStripBuffer ( strip_buf );
return ( NO_ERROR );
}
static void FreeStripBuffer ( unsigned char *buffer )
{
free ( ( void * ) buffer );
}
static void AllocateStripBuffer ( unsigned char **buffer,
struct DataLocation *DataLoc,
int width )
{
unsigned int upper_bd_on_bytes_in_one_strip;
/* compute an upper bound on the number of bytes */
/* for one strip of image data; this is computed */
/* as twice the number of bytes in a strip of */
/* uncompressed color data */
upper_bd_on_bytes_in_one_strip =
( unsigned int ) ( 2 * DataLoc->rows_per_strip * 3 * width );
/* allocate temporary space for a strip of image data */
*buffer = ( unsigned char * )
mget_spc ( ( int ) upper_bd_on_bytes_in_one_strip,
sizeof ( unsigned char ) );
}
static int PutStrip ( FILE *fp, struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned char *strip_buf,
unsigned int strip_index )
{
if ( img->compress_type == 'u' ) {
if ( PackStrip ( img, DataLoc, strip_buf, strip_index ) == ERROR )
return ( ERROR );
}
else {
fprintf ( stderr, "tiff.c: function PutStrip:\n" );
fprintf ( stderr, "TIFF writer not prepared to compress data\n" );
fprintf ( stderr, "with compress_type %c\n", img->compress_type );
return ( ERROR );
}
if ( WriteStrip ( fp, DataLoc, strip_buf, strip_index ) == ERROR )
return ( ERROR );
return ( NO_ERROR );
}
static int PackStrip ( struct TIFF_img *img,
struct DataLocation *DataLoc,
unsigned char *buffer,
unsigned int strip_index )
{
unsigned int i, first_row, bytes_packed, current_row;
int j;
/* compute index of first row for this strip */
first_row = strip_index * DataLoc->rows_per_strip;
bytes_packed = 0;
for ( i = 0; i < DataLoc->rows_per_strip; i++ ) {
current_row = first_row + i;
if ( current_row < ( unsigned int ) img->height )
for ( j = 0; j < img->width; j++ ) {
if ( ( img->TIFF_type == 'g' ) || ( img->TIFF_type == 'p' ) )
buffer[bytes_packed++] = img->mono[current_row][j];
else if ( img->TIFF_type == 'c' ) {
buffer[bytes_packed++] = img->color[0][current_row][j];
buffer[bytes_packed++] = img->color[1][current_row][j];
buffer[bytes_packed++] = img->color[2][current_row][j];
}
else {
fprintf ( stderr, "tiff.c: function PackStrip:\n" );
fprintf ( stderr, "tiff reader not prepared to pack data\n" );
fprintf ( stderr, "for image of TIFF type %c\n", img->TIFF_type );
return ( ERROR );
}
}
}
DataLoc->strip_byte_counts[strip_index] = bytes_packed;
return ( NO_ERROR );
}
static int WriteStrip ( FILE *fp, struct DataLocation *DataLoc,
unsigned char *strip_buf,
unsigned int strip_index )
{
static unsigned int strip_offset;
/* first strip should begin on the eighth byte of */
/* the image file (right after the image header) */
if ( strip_index == 0 ) strip_offset = 8;
/* record offset for strip */
DataLoc->strip_offsets[strip_index] = strip_offset;
/* set file-position at desired location of first byte of strip */
if ( SetFilePosition ( fp, DataLoc->strip_offsets[strip_index] )
== ERROR ) return ( ERROR );
/* write strip of data */
if ( DataLoc->strip_byte_counts[strip_index] !=
fwrite ( ( unsigned char * ) strip_buf, sizeof ( unsigned char ),
( size_t ) DataLoc->strip_byte_counts[strip_index], fp ) ) {
fprintf ( stderr, "tiff.c: function WriteStrip:\n" );
fprintf ( stderr, "error writing strip number %ld\n", strip_index );
return ( ERROR );
}
/* update strip_offset number */
strip_offset += DataLoc->strip_byte_counts[strip_index];
/* store the offset of the byte after the data (this may be the */
/* offset of the IFD, but remember that the IFD has to begin on */
/* a word boundary - this is checked in PrepareHeader) */
DataLoc->offset_of_byte_after_data = strip_offset;
return ( NO_ERROR );
}
static int MakeImageDataLocInfo ( struct TIFF_img *img,
struct DataLocation *DataLoc )
{
/* compute number of rows per strip so that there */
/* are around 8000 bytes of data in each strip */
if ( DetermineRowsPerStrip ( img, DataLoc ) == ERROR ) return ( ERROR );
/* compute number of strips necessary for */
/* storing image, store in DataLoc structure */
if ( ComputeStripsPerImage ( img->height, DataLoc ) == ERROR )
return ( ERROR );
/* allocate arrays for DataLoc structure */
DataLoc->strip_byte_counts = ( unsigned int * )
mget_spc ( ( int ) DataLoc->StripsPerImage,
sizeof ( unsigned int ) );
DataLoc->strip_offsets = ( unsigned int * )
mget_spc ( ( int ) DataLoc->StripsPerImage,
sizeof ( unsigned int ) );
return ( NO_ERROR );
}
static int ComputeStripsPerImage ( int height, struct DataLocation *DataLoc )
{
unsigned int ULheight;
if ( height <= 0 ) {
fprintf ( stderr, "tiff.c: function ComputeStripsPerImage:\n" );
fprintf ( stderr, "height entry in TIFF image structure\n" );
fprintf ( stderr, "claims that image height is not positive\n" );
return ( ERROR );
}
ULheight = ( unsigned int ) height;
if ( ( ULheight % DataLoc->rows_per_strip ) == 0 )
DataLoc->StripsPerImage = ULheight / DataLoc->rows_per_strip;
else
DataLoc->StripsPerImage = 1 + ( unsigned int )
( height / DataLoc->rows_per_strip );
return ( NO_ERROR );
}
static int DetermineRowsPerStrip ( struct TIFF_img *img,
struct DataLocation *DataLoc )
{
unsigned int byte_count;
/**********************************************/
/* determine bytes per row before compression */
/* MAY WANT TO CHANGE THIS WHEN A COMPRESSION */