1
+ function oimg = readTIFstack(path )
2
+
3
+ % Copyright (c) 2012, YoonOh Tak
4
+ % All rights reserved.
5
+
6
+ % Redistribution and use in source and binary forms, with or without
7
+ % modification, are permitted provided that the following conditions are
8
+ % met:
9
+ %
10
+ % * Redistributions of source code must retain the above copyright
11
+ % notice, this list of conditions and the following disclaimer.
12
+ % * Redistributions in binary form must reproduce the above copyright
13
+ % notice, this list of conditions and the following disclaimer in
14
+ % the documentation and/or other materials provided with the distribution
15
+ % * Neither the name of the Gwangju Institute of Science and Technology (GIST), Republic of Korea nor the names
16
+ % of its contributors may be used to endorse or promote products derived
17
+ % from this software without specific prior written permission.
18
+ %
19
+ % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ % IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
+ % ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
+ % LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
+ % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
+ % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
+ % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
+ % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
+ % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
+ % POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ s = warning(' off' , ' all' ); % To ignore unknown TIFF tag.
32
+
33
+ % Frame number
34
+ tiff = Tiff(path , ' r' );
35
+ frame = 0 ;
36
+ while true
37
+ frame = frame + 1 ;
38
+ if tiff .lastDirectory()
39
+ break ;
40
+ end ;
41
+ tiff .nextDirectory();
42
+ end ;
43
+
44
+ k_struct = 0 ;
45
+ tiff .setDirectory(1 );
46
+ for kf = 1 : frame
47
+ if kf == 1
48
+ n1 = tiff .getTag(' ImageWidth' );
49
+ m1 = tiff .getTag(' ImageLength' );
50
+ spp1 = tiff .getTag(' SamplesPerPixel' );
51
+ sf1 = tiff .getTag(' SampleFormat' );
52
+ bpp1 = tiff .getTag(' BitsPerSample' );
53
+ if kf ~= frame
54
+ tiff .nextDirectory();
55
+ end ;
56
+ continue ;
57
+ end ;
58
+
59
+ n2 = tiff .getTag(' ImageWidth' );
60
+ m2 = tiff .getTag(' ImageLength' );
61
+ spp2 = tiff .getTag(' SamplesPerPixel' );
62
+ sf2 = tiff .getTag(' SampleFormat' );
63
+ bpp2 = tiff .getTag(' BitsPerSample' );
64
+
65
+ if n1 ~= n2 || m1 ~= m2 || spp1 ~= spp2 || sf1 ~= sf2 || bpp1 ~= bpp2
66
+ k_struct = k_struct + 1 ;
67
+ tifstruct(k_struct ).m = m1 ;
68
+ tifstruct(k_struct ).n = n1 ;
69
+ tifstruct(k_struct ).spp = spp1 ;
70
+ tifstruct(k_struct ).frame = kf - 1 ;
71
+ tifstruct(k_struct ).data_type = DataType(sf1 , bpp1 );
72
+ end ;
73
+
74
+ if kf ~= frame
75
+ tiff .nextDirectory();
76
+ else
77
+ if k_struct > 0
78
+ k_struct = k_struct + 1 ;
79
+ tifstruct(k_struct ).m = m2 ;
80
+ tifstruct(k_struct ).n = n2 ;
81
+ tifstruct(k_struct ).spp = spp2 ;
82
+ tifstruct(k_struct ).frame = kf ;
83
+ tifstruct(k_struct ).data_type = DataType(sf2 , bpp2 );
84
+ end ;
85
+ end ;
86
+ n1 = n2 ; m1 = m2 ; spp1 = spp2 ; sf1 = sf2 ; bpp1 = bpp2 ;
87
+ end ;
88
+
89
+ if k_struct == 0
90
+ if spp1 == 1
91
+ oimg = zeros(m1 , n1 , frame , DataType(sf1 , bpp1 )); % grayscle
92
+ for kf = 1 : frame
93
+ tiff .setDirectory(kf );
94
+ oimg(: , : , kf ) = tiff .read();
95
+ end ;
96
+ else
97
+ oimg = zeros(m1 , n1 , spp1 , frame , DataType(sf1 , bpp1 )); % color
98
+ for kf = 1 : frame
99
+ tiff .setDirectory(kf );
100
+ oimg(: , : , : , kf ) = tiff .read();
101
+ end ;
102
+ end ;
103
+ else
104
+ k_cell = 1 ;
105
+ kf_start = 1 ;
106
+ for kc= 1 : k_struct
107
+ if tifstruct(kc ).spp == 1
108
+ temp = zeros(tifstruct(kc ).m, tifstruct(kc ).n, tifstruct(kc ).frame- kf_start + 1 , tifstruct(kc ).data_type);
109
+ for kf= 1 : tifstruct(kc ).frame- kf_start + 1
110
+ tiff .setDirectory(kf + kf_start - 1 );
111
+ temp(: , : , kf ) = tiff .read();
112
+ end ;
113
+ oimg{k_cell } = temp ; k_cell = k_cell + 1 ;
114
+ else
115
+ temp = zeros(tifstruct(kc ).m, tifstruct(kc ).n, 3 , tifstruct(kc ).frame- kf_start + 1 , tifstruct(kc ).data_type);
116
+ for kf= 1 : tifstruct(kc ).frame- kf_start + 1
117
+ tiff .setDirectory(kf + kf_start - 1 );
118
+ temp(: , : , : , kf ) = tiff .read();
119
+ end ;
120
+ oimg{k_cell } = temp ; k_cell = k_cell + 1 ;
121
+ end ;
122
+ kf_start = tifstruct(kc ).frame + 1 ;
123
+ end ;
124
+ end ;
125
+
126
+ tiff .close();
127
+
128
+ warning(s );
129
+
130
+ end
131
+
132
+ function out = DataType(sf , bpp )
133
+ switch sf
134
+ case 1
135
+ switch bpp
136
+ case 8
137
+ out = ' uint8' ;
138
+ case 16
139
+ out = ' uint16' ;
140
+ case 32
141
+ out = ' uint32' ;
142
+ end ;
143
+ case 2
144
+ switch bpp
145
+ case 8
146
+ out = ' int8' ;
147
+ case 16
148
+ out = ' int16' ;
149
+ case 32
150
+ out = ' int32' ;
151
+ end ;
152
+ case 3
153
+ switch bpp
154
+ case 32
155
+ out = ' single' ;
156
+ case 64
157
+ out = ' double' ;
158
+ end ;
159
+ end ;
160
+
161
+ end
0 commit comments