File: TIFFImageFileDirectory.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,280 kB
  • ctags: 3,725
  • sloc: java: 31,190; sh: 238; makefile: 53; xml: 30
file content (1114 lines) | stat: -rw-r--r-- 27,162 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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
/*
 * TIFFImageFileDirectory
 * 
 * Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.codecs.tiff;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;
import net.sourceforge.jiu.codecs.InvalidFileStructureException;
import net.sourceforge.jiu.codecs.UnsupportedTypeException;
import net.sourceforge.jiu.codecs.tiff.TIFFConstants;
import net.sourceforge.jiu.codecs.tiff.TIFFTag;
import net.sourceforge.jiu.data.Palette;
import net.sourceforge.jiu.data.RGBIndex;

/**
 * This class encapsulates all data of a TIFF image file directory (IFD).
 * @author Marco Schmidt
 */
public class TIFFImageFileDirectory implements TIFFConstants
{
	public static final int TYPE_BILEVEL_PACKED = 0;
	public static final int TYPE_GRAY4 = 1;
	public static final int TYPE_GRAY8 = 2;
	public static final int TYPE_GRAY16 = 3;
	public static final int TYPE_PALETTED4 = 4;
	public static final int TYPE_PALETTED8 = 5;
	public static final int TYPE_RGB24_INTERLEAVED = 6;
	public static final int TYPE_RGB48_INTERLEAVED = 7;
	public static final int TYPE_BILEVEL_BYTE = 8;
	public static final int TYPE_CMYK32_INTERLEAVED = 9;
	public static final int TYPE_CMYK32_PLANAR = 10;
	public static final int TYPE_LOGLUV32_INTERLEAVED = 11;
	public static final int TYPE_LOGL = 12;

	private String artist;
	private int[] bitsPerSample;
	private int bitsPerPixel;
	private int bitsPerRow;
	private int bytesBetweenSamples;
	private int[] bytesPerSample;
	private int bytesPerRow;
	private int compression;
	private String copyright;
	private Date date;
	private String dateTime;
	private int dpiX;
	private int dpiY;
	private int[] extraSamples;
	private int height;
	private int horizontalTiles;
	private String hostComputer;
	private String imageDescription;
	private int imageType;
	private boolean invertGraySamples;
	private String make;
	private String model;
	private int numStrips;
	private int numTiles;
	private int orientation;
	private Palette palette;
	private int pixelsPerRow;
	private int planarConfiguration;
	private int photometricInterpretation;
	private int predictor;
	private int[] sampleTypes;
	private int resolutionUnit;
	private double resolutionX;
	private double resolutionY;
	private int rowsPerStrip;
	private int samplesPerPixel;
	private String software;
	private Vector stripByteCounts;
	private Vector stripOffsets;
	private int t4Options;
	private int t6Options;
	private Vector tags;
	private Vector tileByteCounts;
	private Vector tileOffsets;
	private TimeZone timeZone;
	private int tileWidth;
	private int tileHeight;
	private int verticalTiles;
	private int width;

	/**
	 * Initializes all members to null or -1 and creates an internal list for
	 * the tags that will be make up this directory.
	 */
	public TIFFImageFileDirectory()
	{
		initMembers();
		tags = new Vector();
	}

	/**
	 * Adds a tag to the end of the internal list of tags.
	 * @param tag the TIFFTag instance to be appended
	 */
	public void append(TIFFTag tag)
	{
		tags.addElement(tag);
	}

	private void checkContent() throws
		InvalidFileStructureException,
		UnsupportedTypeException
	{
		if (width < 1)
		{
			throw new InvalidFileStructureException("No valid width available.");
		}
		if (height < 1)
		{
			throw new InvalidFileStructureException("No valid width available.");
		}
		if (stripOffsets != null)
		{
			pixelsPerRow = width;
		}
		else
		if (tileOffsets != null)
		{
			pixelsPerRow = tileWidth;
		}
		if (rowsPerStrip == -1 && stripOffsets != null && stripOffsets.size() == 1)
		{
			rowsPerStrip = height;
		}
		// do more checks based on color type
		switch (photometricInterpretation)
		{
			case(PHOTOMETRIC_BLACK_IS_ZERO):
			case(PHOTOMETRIC_WHITE_IS_ZERO):
			{
				if (bitsPerSample[0] == 1)
				{
					imageType = TYPE_BILEVEL_PACKED;
				}
				else
				{
					if (bitsPerSample[0] == 4)
					{
						imageType = TYPE_GRAY4;
					}
					else
					if (bitsPerSample[0] == 8)
					{
						imageType = TYPE_GRAY8;
					}
					else
					{
						throw new UnsupportedTypeException("Only bit depths 1, 4 and 8 are supported for bilevel and grayscale images.");
					}
				}
				break;
			}
			case(PHOTOMETRIC_PALETTED):
			{
				if (getPalette() == null)
				{
					throw new InvalidFileStructureException("No palette found in paletted image.");
				}
				break;
			}
			case(PHOTOMETRIC_TRUECOLOR_RGB):
			{
				if (planarConfiguration != PLANAR_CONFIGURATION_CHUNKY)
				{
					throw new UnsupportedTypeException("Cannot handle planar configuration other than chunky for RGB images.");
				}
				if (bitsPerSample.length != 3)
				{
					throw new UnsupportedTypeException("Found RGB truecolor image, but instead of three " + bitsPerSample.length + " component(s).");
				}
				if (bitsPerPixel == 24)
				{
					imageType = TYPE_RGB24_INTERLEAVED;
				}
				else
				if (bitsPerPixel == 48)
				{
					imageType = TYPE_RGB48_INTERLEAVED;
				}
				else
				{
					throw new UnsupportedTypeException("Unsupported RGB truecolor image color depth: " + bitsPerPixel + ".");
				}
				break;
			}
			case(PHOTOMETRIC_TRUECOLOR_LOGLUV):
			{
				if (planarConfiguration == PLANAR_CONFIGURATION_CHUNKY)
				{
					imageType = TYPE_LOGLUV32_INTERLEAVED;
				}
				else
				{
					throw new UnsupportedTypeException("Cannot handle planar configuration other than chunky for RGB images.");
				}
				break;
			}
			case(PHOTOMETRIC_LOGL):
			{
				imageType = TYPE_LOGL;
				break;
			}
			case(PHOTOMETRIC_TRUECOLOR_CMYK):
			{
				if (planarConfiguration == PLANAR_CONFIGURATION_CHUNKY)
				{
					imageType = TYPE_CMYK32_INTERLEAVED;
				}
				/*else
				if (planarConfiguration == PLANAR_CONFIGURATION_PLANAR)
				{
					imageType = TYPE_CMYK32_PLANAR;
				}*/
				else
				{
					throw new UnsupportedTypeException("Cannot handle planar configuration other than chunky for CMYK images.");
				}
				break;
			}
			default:
			{
				throw new UnsupportedTypeException("Unsupported color type: "  + photometricInterpretation + ".");
			}
		}
		if (compression == COMPRESSION_CCITT_GROUP3_1D_MODIFIED_HUFFMAN)
		{
			if (bitsPerPixel != 1)
			{
				throw new UnsupportedTypeException("Number of bits per pixel must be 1 for " +
					"compression type: " + getCompressionName(compression) + ".");
			}
			imageType = TYPE_BILEVEL_BYTE;
		}
		// TODO more validity checks
	}

	/**
	 * TODO: regard extra samples
	 */
	public int computeNumBytes(int numPixels)
	{
		if (bitsPerPixel == 1)
		{
			if (imageType == TYPE_BILEVEL_BYTE)
			{
				return numPixels;
			}
			else
			{
				return (numPixels + 7) / 8;
			}
		}
		else
		if (bitsPerPixel <= 4)
		{
			return (numPixels + 1) / 2;
		}
		else
		if (bitsPerPixel <= 8)
		{
			return numPixels;
		}
		else
		if (bitsPerPixel == 16)
		{
			return numPixels * 2;
		}
		else
		if (bitsPerPixel == 24)
		{
			return numPixels * 3;
		}
		else
		if (bitsPerPixel == 32)
		{
			return numPixels * 4;
		}
		else
		if (bitsPerPixel == 48)
		{
			return numPixels * 6;
		}
		else
		{
			return -1;
		}
	}

	/**
	 * Returns information on the person who created the image
	 * (as stored in tag {@link TIFFConstants#TAG_ARTIST}).
	 */
	public String getArtist()
	{
		return artist;
	}

	/**
	 * Returns the number of bits per pixel (not including transparency information).
	 */
	public int getBitsPerPixel()
	{
		return bitsPerPixel;
	}

	/**
	 * Returns the number of compressed byte for a given tile.
	 * Tile index must not be negative and must be smaller than the number of tiles.
	 * @param tileIndex zero-based index of tile or strip for which the number of compressed bytes is to be returned
	 */
	public int getByteCount(int tileIndex)
	{
		if (stripByteCounts != null)
		{
			return ((Number)stripByteCounts.elementAt(tileIndex)).intValue();
		}
		else
		if (tileByteCounts != null)
		{
			return ((Number)tileByteCounts.elementAt(tileIndex)).intValue();
		}
		else
		{
			return 0;
		}
	}

	public int getBytesPerRow()
	{
		return computeNumBytes(getTileWidth());
	}

	/**
	 * Returns the compression method, encoded as a number as found in
	 * {@link TIFFConstants} (more specifically, the COMPRESSION_xyz constants).
	 * Use {@link #getCompressionName(int)} to get the English name
	 * of this compression method.
	 * @return compression method 
	 */
	public int getCompression()
	{
		return compression;
	}

	/**
	 * Returns the name of a TIFF compression method.
	 * If the name is unknown, <em>Unknown method</em> plus
	 * the method number is returned.
	 * This static method can be used in combination with the value from
	 * {@link #getCompression}.
	 * @param method the compression method number
	 * @return the compression method name
	 */
	public static String getCompressionName(int method)
	{
		switch(method)
		{
			case(COMPRESSION_CCITT_GROUP3_1D_MODIFIED_HUFFMAN): return "CCITT Group 3 1D Modified Huffman";
			case(COMPRESSION_CCITT_T4): return "CCITT T.4";
			case(COMPRESSION_CCITT_T6): return "CCITT T.6";
			case(COMPRESSION_DEFLATED_INOFFICIAL): return "Deflated (inofficial, 32496)";
			case(COMPRESSION_DEFLATED_OFFICIAL): return "Deflated (official, 8)";
			case(COMPRESSION_LZW): return "LZW";
			case(COMPRESSION_NONE): return "Uncompressed";
			case(COMPRESSION_PACKBITS): return "Packbits";
			case(6): return "JPEG (old style)";
			case(7):return "JPEG (new style)";
			case(103): return "Pegasus IMJ";
			case(32766): return "NeXT 2-bit RLE";
			case(32771): return "Uncompressed, word-aligned";
			case(32809): return "Thunderscan RLE";
			case(32895): return "IT8 CT with padding";
			case(32896): return "IT8 Linework RLE";
			case(32897): return "IT8 Monochrome picture";
			case(32898): return "IT8 Binary line art";
			case(32908): return "Pixar 10 bit LZW";
			case(32909): return "Pixar 11 bit ZIP";
			case(32947): return "Kodak DCS";
			case(34661): return "ISO JBIG";
			case(34676): return "SGI Log Luminance RLE";
			case(34677): return "SGI Log 24-bit packed";
			default: return "Unknown method (" + method + ")";
		}
	}

	public String getCopyright()
	{
		return copyright;
	}

	/**
	 * If a date / time tag was found in this image file directory and
	 * {@link #initFromTags} was called already, it was attempted to
	 * create a {@link java.util.Date} object from it.
	 * This object (or <code>null</code>) is returned.
	 * Use {@link #setTimeZone} to provide a time zone before the date 
	 * parsing is done.
	 * @see #getDateTimeString
	 */
	public Date getDateTime()
	{
		return date;
	}

	/**
	 * If there was a date / time tag in this IFD, its String value
	 * is returned.
	 * @see #getDateTime
	 */
	public String getDateTimeString()
	{
		return dateTime;
	}

	public int getDpiX()
	{
		return dpiX;
	}

	public int getDpiY()
	{
		return dpiY;
	}

	public int getHeight()
	{
		return height;
	}

	public String getHostComputer()
	{
		return hostComputer;
	}

	public String getImageDescription()
	{
		return imageDescription;
	}

	public int getImageType()
	{
		return imageType;
	}

	public String getModel()
	{
		return model;
	}

	public int getNumHorizontalTiles()
	{
		return horizontalTiles;
	}

	public int getNumStrips()
	{
		return numStrips;
	}

	public int getNumTiles()
	{
		return numTiles;
	}

	public int getNumVerticalTiles()
	{
		return verticalTiles;
	}

	public Palette getPalette()
	{
		return palette;
	}

	public int getPhotometricInterpretation()
	{
		return photometricInterpretation;
	}

	public int getPredictor()
	{
		return predictor;
	}

	public int getRowsPerStrip()
	{
		return rowsPerStrip;
	}

	public int getSamplesPerPixel()
	{
		return samplesPerPixel;
	}

	public String getSoftware()
	{
		return software;
	}

	public Vector getStripOffsets()
	{
		return stripOffsets;
	}

	public int getT4Options()
	{
		return t4Options;
	}

	public int getT6Options()
	{
		return t6Options;
	}

	public int getTileHeight()
	{
		return tileHeight;
	}

	public long getTileOffset(int tileIndex)
	{
		if (stripOffsets != null)
		{
			Number number = (Number)stripOffsets.elementAt(tileIndex);
			return number.longValue();
		}
		else
		if (tileOffsets != null)
		{
			Number number = (Number)tileOffsets.elementAt(tileIndex);
			return number.longValue();
		}
		else
		{
			throw new IllegalArgumentException("Tile index invalid: " + tileIndex);
		}
	}

	public int getTileWidth()
	{
		return tileWidth;
	}

	public int getTileX1(int tileIndex)
	{
		if (tileIndex < 0 || tileIndex >= getNumTiles())
		{
			throw new IllegalArgumentException("Not a valid tile index: "  + tileIndex);
		}
		else
		{
			return (tileIndex % getNumHorizontalTiles()) * getTileWidth();
		}
	}

	public int getTileX2(int tileIndex)
	{
		if (tileIndex < 0 || tileIndex >= getNumTiles())
		{
			throw new IllegalArgumentException("Not a valid tile index: "  + tileIndex);
		}
		else
		{
			return ((tileIndex % getNumHorizontalTiles()) + 1) * getTileWidth() - 1;
		}
	}

	public int getTileY1(int tileIndex)
	{
		if (tileIndex < 0 || tileIndex >= getNumTiles())
		{
			throw new IllegalArgumentException("Not a valid tile index: "  + tileIndex);
		}
		else
		{
			return (tileIndex % getNumVerticalTiles()) * getTileHeight();
		}
	}

	public int getTileY2(int tileIndex)
	{
		if (tileIndex < 0 || tileIndex >= getNumTiles())
		{
			throw new IllegalArgumentException("Not a valid tile index: "  + tileIndex);
		}
		else
		{
			int result = ((tileIndex % getNumVerticalTiles()) + 1) * getTileHeight() - 1;
			if (result >= height)
			{
				result = height - 1;
			}
			return result;
		}
	}

	public int getWidth()
	{
		return width;
	}

	public void initMembers()
	{
		bitsPerPixel = -1;
		bitsPerSample = null;
		compression = -1;
		height = -1;
		horizontalTiles = -1;
		invertGraySamples = false;
		numStrips = -1;
		numTiles = -1;
		orientation = 1;
		photometricInterpretation = -1;
		planarConfiguration = -1;
		resolutionUnit = 2;
		resolutionX = -1.0;
		resolutionY = -1.0;
		rowsPerStrip = -1;
		stripOffsets = null;
		tags = null;
		tileOffsets = null;
		tileWidth = -1;
		tileHeight = -1;
		verticalTiles = -1;
		width = -1;
	}

	public void initFromTags(boolean check) throws
		InvalidFileStructureException,
		UnsupportedTypeException
	{
		int index = 0;
		while (index < tags.size())
		{
			TIFFTag tag = (TIFFTag)tags.elementAt(index++);
			int id = tag.getId();
			int count = tag.getCount();
			int type = tag.getType();
			boolean isNotInt = !tag.isInt();
			switch(id)
			{
				case(TAG_ARTIST):
				{
					artist = tag.getString();
					break;
				}
				case(TAG_BITS_PER_SAMPLE):
				{
					if (isNotInt)
					{
						throw new InvalidFileStructureException("Bits per " +
							"sample value(s) must be byte/short/long; type=" +
							type);
					}
					if (count == 1)
					{
						bitsPerSample = new int[1];
						bitsPerSample[0] = tag.getOffset();
						bitsPerPixel = bitsPerSample[0];
					}
					else
					{
						bitsPerPixel = 0;
						bitsPerSample = new int[count];
						for (int i = 0; i < count; i++)
						{
							bitsPerSample[i] = tag.getElementAsInt(i);
							if (bitsPerSample[i] < 1)
							{
								throw new InvalidFileStructureException("Bits per " +
									"sample value #" + i + " is smaller than 1.");
							}
							bitsPerPixel += bitsPerSample[i];
						}
					}
					break;
				}
				case(TAG_COLOR_MAP):
				{
					if ((count % 3) != 0)
					{
						throw new InvalidFileStructureException("Number of palette entries must be divideable by three without rest; " + count);
					}
					if (count < 3 || count > 768)
					{
						throw new UnsupportedTypeException("Unsupported number of palette entries: " + count + ".");
					}
					if (type != TAG_TYPE_SHORT)
					{
						throw new UnsupportedTypeException("Unsupported number type for palette entries: " + type);
					}
					int numEntries = count / 3;
					palette = new Palette(numEntries, 255);
					int vectorIndex = 0;
					for (int paletteIndex = 0; paletteIndex < numEntries; paletteIndex++)
					{
						palette.putSample(RGBIndex.INDEX_RED, paletteIndex, tag.getElementAsInt(vectorIndex++) >> 8);
					}
					for (int paletteIndex = 0; paletteIndex < numEntries; paletteIndex++)
					{
						palette.putSample(RGBIndex.INDEX_GREEN, paletteIndex, tag.getElementAsInt(vectorIndex++) >> 8);
					}
					for (int paletteIndex = 0; paletteIndex < numEntries; paletteIndex++)
					{
						palette.putSample(RGBIndex.INDEX_BLUE, paletteIndex, tag.getElementAsInt(vectorIndex++) >> 8);
					}
					break;
				}
				case(TAG_COMPRESSION):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							" single byte/short/long value for compression " +
							"(count=" + count + ", type=" + type + ").");
					}
					compression = tag.getOffset();
					break;
				}
				case(TAG_DATE_TIME):
				{
					dateTime = tag.getString();
					if (dateTime != null)
					{
						dateTime = dateTime.trim();
					}
					if (dateTime != null)
					{
						SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
						if (timeZone != null)
						{
							format.setCalendar(new GregorianCalendar(timeZone));
						}
						try
						{
							date = format.parse(dateTime);
						}
						catch (ParseException pe)
						{
							date = null;
						}
					}
					break;
				}
				case(TAG_HOST_COMPUTER):
				{
					hostComputer = tag.getString();
					break;
				}
				case(TAG_IMAGE_DESCRIPTION):
				{
					imageDescription = tag.getString();
					break;
				}
				case(TAG_IMAGE_WIDTH):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image width " +
							"(count=" + count + ", type=" + type + ").");
					}
					width = tag.getOffset();
					break;
				}
				case(TAG_IMAGE_LENGTH):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image height " +
							"(count=" + count + ", type=" + type + ").");
					}
					height = tag.getOffset();
					break;
				}
				case(TAG_MAKE):
				{
					make = tag.getString();
					break;
				}
				case(TAG_MODEL):
				{
					model = tag.getString();
					break;
				}
				case(TAG_ORIENTATION):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image height " +
							"(count=" + count + ", type=" + type + ").");
					}
					orientation = tag.getOffset();
					break;
				}
				case(TAG_PHOTOMETRIC_INTERPRETATION):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for photometric interpretation.");
					}
					photometricInterpretation = tag.getOffset();
					break;
				}
				case(TAG_RESOLUTION_UNIT):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for planar configuration.");
					}
					resolutionUnit = tag.getOffset();
					break;
				}
				case(TAG_RESOLUTION_X):
				{
					if (count != 1 || type != TAG_TYPE_RATIONAL)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for planar configuration.");
					}
					Object o = tag.getObject(0);
					if (o != null && o instanceof TIFFRational)
					{
						TIFFRational rational = (TIFFRational)o;
						resolutionX = rational.getAsDouble();
					}
					break;
				}
				case(TAG_RESOLUTION_Y):
				{
					if (count != 1 || type != TAG_TYPE_RATIONAL)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for planar configuration.");
					}
					Object o = tag.getObject(0);
					if (o != null && o instanceof TIFFRational)
					{
						TIFFRational rational = (TIFFRational)o;
						resolutionY = rational.getAsDouble();
					}
					break;
				}
				case(TAG_PLANAR_CONFIGURATION):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for planar configuration.");
					}
					planarConfiguration = tag.getOffset();
					break;
				}
				case(TAG_ROWS_PER_STRIP):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image height.");
					}
					rowsPerStrip = tag.getOffset();
					break;
				}
				case(TAG_SAMPLES_PER_PIXEL):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for samples per pixel.");
					}
					samplesPerPixel = tag.getOffset();
					break;
				}
				case(TAG_SOFTWARE):
				{
					software = tag.getString();
					break;
				}
				case(TAG_STRIP_BYTE_COUNTS):
				{
					if (count < 1)
					{
						throw new InvalidFileStructureException("Need at least one strip offset.");
					}
					if (count == 1)
					{
						if (isNotInt)
						{
							throw new InvalidFileStructureException("There is " +
								"only one strip offset, but its type is not integer.");
						}
						stripByteCounts = new Vector();
						stripByteCounts.addElement(new Long(tag.getOffset()));
					}
					else
					{
						stripByteCounts = tag.getVector();
					}
					break;
				}
				case(TAG_STRIP_OFFSETS):
				{
					if (count < 1)
					{
						throw new InvalidFileStructureException("Need at least one strip offset.");
					}
					if (count == 1)
					{
						if (isNotInt)
						{
							throw new InvalidFileStructureException("There is " +
								"only one strip offset, but its type is not integer.");
						}
						stripOffsets = new Vector();
						stripOffsets.addElement(new Long(tag.getOffset()));
					}
					else
					{
						stripOffsets = tag.getVector();
					}
					numStrips = count;
					numTiles = count;
					horizontalTiles = 1;
					verticalTiles = count;
					break;
				}
				case(TAG_T4_OPTIONS):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for T4 Options.");
					}
					t4Options = tag.getOffset();
					break;
				}
				case(TAG_T6_OPTIONS):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for T6 Options.");
					}
					t6Options = tag.getOffset();
					break;
				}
				case(TAG_TILE_HEIGHT):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image height " +
							"(count=" + count + ", type=" + type + ").");
					}
					tileHeight = tag.getOffset();
					if (tileHeight < 1)
					{
						throw new InvalidFileStructureException("Tile height must be one or larger.");
					}
					verticalTiles = height / tileHeight;
					if ((height % tileHeight) != 0)
					{
						verticalTiles++;
					}
					break;
				}
				case(TAG_TILE_OFFSETS):
				{
					if (count < 1)
					{
						throw new InvalidFileStructureException("Need at least one tile offset.");
					}
					if (count == 1)
					{
						if (isNotInt)
						{
							throw new InvalidFileStructureException("There is " +
								"only one tile offset, but its type is not integer.");
						}
						tileOffsets = new Vector();
						tileOffsets.addElement(new Long(tag.getOffset()));
					}
					else
					{
						tileOffsets = tag.getVector();
					}
					numStrips = count;
					numTiles = count;
					horizontalTiles = 1;
					verticalTiles = count;
					break;
				}
				case(TAG_TILE_WIDTH):
				{
					if (count != 1 || isNotInt)
					{
						throw new InvalidFileStructureException("Expected " +
							"single byte/short/long value for image height " +
							"(count=" + count + ", type=" + type + ").");
					}
					tileWidth = tag.getOffset();
					if (tileWidth < 1)
					{
						throw new InvalidFileStructureException("Tile width must be one or larger.");
					}
					horizontalTiles = width / tileWidth;
					if ((width % tileWidth) != 0)
					{
						horizontalTiles++;
					}
					break;
				}
			}
		}
		if (planarConfiguration == -1)
		{
			planarConfiguration = PLANAR_CONFIGURATION_CHUNKY;
		}
		if (photometricInterpretation == TIFFConstants.PHOTOMETRIC_PALETTED)
		{
			if (bitsPerPixel == 4)
			{
				imageType = TYPE_PALETTED4;
			}
			else
			if (bitsPerPixel == 8)
			{
				imageType = TYPE_PALETTED8;
			}
			else
			{
				throw new UnsupportedTypeException("Only paletted images with 4 or 8 bits per sample are supported.");
			}
		}
		if (resolutionUnit == 2 && resolutionX > 0.0 && resolutionY > 0.0)
		{
			dpiX = (int)resolutionX;
			dpiY = (int)resolutionY;
		}
		if (isStriped())
		{
			tileWidth = width;
			if (numStrips == 1 && rowsPerStrip == -1)
			{
				rowsPerStrip = height;
			}
			tileHeight = rowsPerStrip;
		}
		if (check)
		{
			checkContent();
		}
	}

	public boolean isGrayscale()
	{
		return getBitsPerPixel() > 1 && 
			(photometricInterpretation == PHOTOMETRIC_BLACK_IS_ZERO || 
			 photometricInterpretation == PHOTOMETRIC_WHITE_IS_ZERO);
	}

	public boolean isPaletted()
	{
		return (photometricInterpretation == PHOTOMETRIC_PALETTED);
	}

	/**
	 * Returns <code>true</code> if the image belonging to this IFD
	 * is stored as strips, <code>false</code> otherwise.
	 * @see #isTiled
	 */
	public boolean isStriped()
	{
		return (stripOffsets != null);
	}

	/**
	 * Returns <code>true</code> if the image belonging to this IFD
	 * is stored as tiles, <code>false</code> otherwise.
	 * @see #isStriped
	 */
	public boolean isTiled()
	{
		return (tileOffsets != null);
	}

	/**
	 * Sets the time zone to be used when trying to interpret dates
	 * found in a {@link #TAG_DATE_TIME} tag.
	 * Example call: 
	 * <code>setTimeZone(TimeZone.getTimeZone("America/New_York");</code>.
	 * @param tz TimeZone object
	 */
	public void setTimeZone(TimeZone tz)
	{
		timeZone = tz;
	}
}