1 | # -*- coding: utf-8 -*- |
---|
2 | ########### SVN repository information ################### |
---|
3 | # $Date: 2014-12-27 11:14:59 -0600 (Sat, 27 Dec 2014) $ |
---|
4 | # $Author: $ |
---|
5 | # $Revision: $ |
---|
6 | # $URL: $ |
---|
7 | # $Id: $ |
---|
8 | ########### SVN repository information ################### |
---|
9 | ''' |
---|
10 | *Module G2img_TIF: .tif image file* |
---|
11 | -------------------------------------- |
---|
12 | |
---|
13 | Routine to read an image in Tagged-image file (TIF) format as well as a variety |
---|
14 | of slightly incorrect pseudo-TIF formats used at instruments around the world. |
---|
15 | |
---|
16 | ''' |
---|
17 | |
---|
18 | import sys |
---|
19 | import struct as st |
---|
20 | import os.path as ospath |
---|
21 | import GSASIIIO as G2IO |
---|
22 | import GSASIIpath |
---|
23 | import numpy as np |
---|
24 | DEBUG = False |
---|
25 | GSASIIpath.SetVersionNumber("$Revision: $") |
---|
26 | class TIF_ReaderClass(G2IO.ImportImage): |
---|
27 | '''Routine to read an image in Tagged-image file (TIF) format as well as a variety |
---|
28 | of slightly incorrect pseudo-TIF formats |
---|
29 | ''' |
---|
30 | def __init__(self): |
---|
31 | super(self.__class__,self).__init__( # fancy way to self-reference |
---|
32 | extensionlist=('.tif','.tiff'), |
---|
33 | strictExtension=False, |
---|
34 | formatName = 'TIF image', |
---|
35 | longFormatName = 'Various .tif and pseudo-TIF formats' |
---|
36 | ) |
---|
37 | |
---|
38 | def ContentsValidator(self, filepointer): |
---|
39 | '''Does the header match the required TIF header? |
---|
40 | ''' |
---|
41 | tag = filepointer.read(2) |
---|
42 | if tag == 'II' and int(st.unpack('<h',filepointer.read(2))[0]) == 42: #little endian |
---|
43 | pass |
---|
44 | elif tag == 'MM' and int(st.unpack('>h',filepointer.read(2))[0]) == 42: #big endian |
---|
45 | pass |
---|
46 | else: |
---|
47 | return False # header not found; not valid TIF |
---|
48 | return True |
---|
49 | |
---|
50 | def Reader(self,filename,filepointer, ParentFrame=None, **unused): |
---|
51 | '''Read the TIF file using Bob's routine |
---|
52 | ''' |
---|
53 | |
---|
54 | self.Comments,self.Data,self.Npix,self.Image = GetTifData(filename) |
---|
55 | if self.Npix == 0: |
---|
56 | print("GetTifData failed to read "+str(filename)+" Trying SciPy") |
---|
57 | import scipy.misc |
---|
58 | self.Image = scipy.misc.imread(filename,flatten=True) |
---|
59 | self.Npix = self.Image.size |
---|
60 | if ParentFrame: |
---|
61 | self.Comments = ['no metadata'] |
---|
62 | self.Data = {'wavelength': 0.1, 'pixelSize': [200, 200], 'distance': 100.0} |
---|
63 | self.Data['size'] = list(self.Image.shape) |
---|
64 | self.Data['center'] = [int(i/2) for i in self.Image.shape] |
---|
65 | G2IO.EditImageParms(ParentFrame,self.Data,self.Comments,self.Image,filename) |
---|
66 | if self.Npix == 0: |
---|
67 | return False |
---|
68 | self.LoadImage(ParentFrame,filename) |
---|
69 | return True |
---|
70 | |
---|
71 | def GetTifData(filename,imageOnly=False): |
---|
72 | '''Read an image in a pseudo-tif format, |
---|
73 | as produced by a wide variety of software, almost always |
---|
74 | incorrectly in some way. |
---|
75 | ''' |
---|
76 | import struct as st |
---|
77 | import array as ar |
---|
78 | import ReadMarCCDFrame as rmf |
---|
79 | image = None |
---|
80 | File = open(filename,'rb') |
---|
81 | dataType = 5 |
---|
82 | center = [None,None] |
---|
83 | wavelength = None |
---|
84 | distance = None |
---|
85 | try: |
---|
86 | Meta = open(filename+'.metadata','Ur') |
---|
87 | head = Meta.readlines() |
---|
88 | for line in head: |
---|
89 | line = line.strip() |
---|
90 | if 'dataType=' in line: |
---|
91 | dataType = int(line.split('=')[1]) |
---|
92 | Meta.close() |
---|
93 | except IOError: |
---|
94 | print 'no metadata file found - will try to read file anyway' |
---|
95 | head = ['no metadata file found',] |
---|
96 | |
---|
97 | tag = File.read(2) |
---|
98 | byteOrd = '<' |
---|
99 | if tag == 'II' and int(st.unpack('<h',File.read(2))[0]) == 42: #little endian |
---|
100 | IFD = int(st.unpack(byteOrd+'i',File.read(4))[0]) |
---|
101 | elif tag == 'MM' and int(st.unpack('>h',File.read(2))[0]) == 42: #big endian |
---|
102 | byteOrd = '>' |
---|
103 | IFD = int(st.unpack(byteOrd+'i',File.read(4))[0]) |
---|
104 | else: |
---|
105 | lines = ['not a detector tiff file',] |
---|
106 | return lines,0,0,0 |
---|
107 | File.seek(IFD) #get number of directory entries |
---|
108 | NED = int(st.unpack(byteOrd+'h',File.read(2))[0]) |
---|
109 | IFD = {} |
---|
110 | nSlice = 1 |
---|
111 | for ied in range(NED): |
---|
112 | Tag,Type = st.unpack(byteOrd+'Hh',File.read(4)) |
---|
113 | nVal = st.unpack(byteOrd+'i',File.read(4))[0] |
---|
114 | if DEBUG: print 'Try:',Tag,Type,nVal |
---|
115 | if Type == 1: |
---|
116 | Value = st.unpack(byteOrd+nVal*'b',File.read(nVal)) |
---|
117 | elif Type == 2: |
---|
118 | Value = st.unpack(byteOrd+'i',File.read(4)) |
---|
119 | elif Type == 3: |
---|
120 | Value = st.unpack(byteOrd+nVal*'h',File.read(nVal*2)) |
---|
121 | x = st.unpack(byteOrd+nVal*'h',File.read(nVal*2)) |
---|
122 | elif Type == 4: |
---|
123 | if Tag in [273,279]: |
---|
124 | nSlice = nVal |
---|
125 | nVal = 1 |
---|
126 | Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4)) |
---|
127 | elif Type == 5: |
---|
128 | Value = st.unpack(byteOrd+nVal*'i',File.read(nVal*4)) |
---|
129 | elif Type == 11: |
---|
130 | Value = st.unpack(byteOrd+nVal*'f',File.read(nVal*4)) |
---|
131 | IFD[Tag] = [Type,nVal,Value] |
---|
132 | if DEBUG: print Tag,IFD[Tag] |
---|
133 | sizexy = [IFD[256][2][0],IFD[257][2][0]] |
---|
134 | [nx,ny] = sizexy |
---|
135 | Npix = nx*ny |
---|
136 | if 34710 in IFD: |
---|
137 | if not imageOnly: |
---|
138 | print 'Read MAR CCD tiff file: ',filename |
---|
139 | marFrame = rmf.marFrame(File,byteOrd,IFD) |
---|
140 | image = np.flipud(np.array(np.asarray(marFrame.image),dtype=np.int32)) |
---|
141 | tifType = marFrame.filetitle |
---|
142 | pixy = [marFrame.pixelsizeX/1000.0,marFrame.pixelsizeY/1000.0] |
---|
143 | head = marFrame.outputHead() |
---|
144 | # extract resonable wavelength from header |
---|
145 | wavelength = marFrame.sourceWavelength*1e-5 |
---|
146 | wavelength = (marFrame.opticsWavelength > 0) and marFrame.opticsWavelength*1e-5 or wavelength |
---|
147 | wavelength = (wavelength <= 0) and None or wavelength |
---|
148 | # extract resonable distance from header |
---|
149 | distance = (marFrame.startXtalToDetector+marFrame.endXtalToDetector)*5e-4 |
---|
150 | distance = (distance <= 0) and marFrame.xtalToDetector*1e-3 or distance |
---|
151 | distance = (distance <= 0) and None or distance |
---|
152 | # extract resonable center from header |
---|
153 | center = [marFrame.beamX*marFrame.pixelsizeX*1e-9,marFrame.beamY*marFrame.pixelsizeY*1e-9] |
---|
154 | center = (center[0] != 0 and center[1] != 0) and center or [None,None] |
---|
155 | #print head,tifType,pixy |
---|
156 | elif nSlice > 1: #CheMin multislice tif file! |
---|
157 | try: |
---|
158 | import Image as Im |
---|
159 | except ImportError: |
---|
160 | try: |
---|
161 | from PIL import Image as Im |
---|
162 | except ImportError: |
---|
163 | print "PIL/pillow Image module not present. This TIF cannot be read without this" |
---|
164 | #raise Exception("PIL/pillow Image module not found") |
---|
165 | lines = ['not a detector tiff file',] |
---|
166 | return lines,0,0,0 |
---|
167 | tifType = 'CheMin' |
---|
168 | pixy = [40,40] |
---|
169 | image = np.flipud(np.array(Im.open(filename)))*10. |
---|
170 | distance = 18.0 |
---|
171 | center = [pixy[0]*sizexy[0]/2000,0] #the CheMin beam stop is here |
---|
172 | wavelength = 1.78892 |
---|
173 | elif 272 in IFD: |
---|
174 | ifd = IFD[272] |
---|
175 | File.seek(ifd[2][0]) |
---|
176 | S = File.read(ifd[1]) |
---|
177 | if 'PILATUS' in S: |
---|
178 | tifType = 'Pilatus' |
---|
179 | dataType = 0 |
---|
180 | pixy = [172,172] |
---|
181 | File.seek(4096) |
---|
182 | if not imageOnly: |
---|
183 | print 'Read Pilatus tiff file: ',filename |
---|
184 | image = ar.array('L',File.read(4*Npix)) |
---|
185 | image = np.array(np.asarray(image),dtype=np.int32) |
---|
186 | else: |
---|
187 | if IFD[258][2][0] == 16: |
---|
188 | tifType = 'GE' |
---|
189 | pixy = [200,200] |
---|
190 | File.seek(8) |
---|
191 | if not imageOnly: |
---|
192 | print 'Read GE-detector tiff file: ',filename |
---|
193 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
194 | elif IFD[258][2][0] == 32: |
---|
195 | tifType = 'CHESS' |
---|
196 | pixy = [200,200] |
---|
197 | File.seek(8) |
---|
198 | if not imageOnly: |
---|
199 | print 'Read CHESS-detector tiff file: ',filename |
---|
200 | image = np.array(ar.array('L',File.read(4*Npix)),dtype=np.int32) |
---|
201 | elif 270 in IFD: |
---|
202 | File.seek(IFD[270][2][0]) |
---|
203 | S = File.read(IFD[273][2][0]-IFD[270][2][0]) |
---|
204 | if 'ImageJ' in S: |
---|
205 | tifType = 'ImageJ' |
---|
206 | dataType = 0 |
---|
207 | pixy = [200,200]*IFD[277][2][0] |
---|
208 | File.seek(IFD[273][2][0]) |
---|
209 | if not imageOnly: |
---|
210 | print 'Read ImageJ tiff file: ',filename |
---|
211 | image = ar.array('H',File.read(2*Npix)) |
---|
212 | if '>' in byteOrd: |
---|
213 | image.byteswap() |
---|
214 | image = np.array(np.asarray(image,dtype='H'),dtype=np.int32) |
---|
215 | elif 262 in IFD and IFD[262][2][0] > 4: |
---|
216 | tifType = 'DND' |
---|
217 | pixy = [158,158] |
---|
218 | File.seek(512) |
---|
219 | if not imageOnly: |
---|
220 | print 'Read DND SAX/WAX-detector tiff file: ',filename |
---|
221 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
222 | elif sizexy == [1536,1536]: |
---|
223 | tifType = 'APS Gold' |
---|
224 | pixy = [150,150] |
---|
225 | File.seek(64) |
---|
226 | if not imageOnly: |
---|
227 | print 'Read Gold tiff file:',filename |
---|
228 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
229 | elif sizexy == [2048,2048] or sizexy == [1024,1024] or sizexy == [3072,3072]: |
---|
230 | if IFD[273][2][0] == 8: |
---|
231 | if IFD[258][2][0] == 32: |
---|
232 | tifType = 'PE' |
---|
233 | pixy = [200,200] |
---|
234 | File.seek(8) |
---|
235 | if not imageOnly: |
---|
236 | print 'Read APS PE-detector tiff file: ',filename |
---|
237 | if dataType == 5: |
---|
238 | image = np.array(ar.array('f',File.read(4*Npix)),dtype=np.float32) |
---|
239 | else: |
---|
240 | image = np.array(ar.array('I',File.read(4*Npix)),dtype=np.int32) |
---|
241 | elif IFD[258][2][0] == 16: |
---|
242 | tifType = 'MedOptics D1' |
---|
243 | pixy = [46.9,46.9] |
---|
244 | File.seek(8) |
---|
245 | if not imageOnly: |
---|
246 | print 'Read MedOptics D1 tiff file: ',filename |
---|
247 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
248 | |
---|
249 | elif IFD[273][2][0] == 4096: |
---|
250 | if sizexy[0] == 3072: |
---|
251 | pixy = [73,73] |
---|
252 | tifType = 'MAR225' |
---|
253 | else: |
---|
254 | pixy = [158,158] |
---|
255 | tifType = 'MAR325' |
---|
256 | File.seek(4096) |
---|
257 | if not imageOnly: |
---|
258 | print 'Read MAR CCD tiff file: ',filename |
---|
259 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
260 | elif IFD[273][2][0] == 512: |
---|
261 | tiftype = '11-ID-C' |
---|
262 | pixy = [200,200] |
---|
263 | File.seek(512) |
---|
264 | if not imageOnly: |
---|
265 | print 'Read 11-ID-C tiff file: ',filename |
---|
266 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
267 | |
---|
268 | elif sizexy == [4096,4096]: |
---|
269 | if IFD[273][2][0] == 8: |
---|
270 | if IFD[258][2][0] == 16: |
---|
271 | tifType = 'scanCCD' |
---|
272 | pixy = [9,9] |
---|
273 | File.seek(8) |
---|
274 | if not imageOnly: |
---|
275 | print 'Read APS scanCCD tiff file: ',filename |
---|
276 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
277 | elif IFD[273][2][0] == 4096: |
---|
278 | tifType = 'Rayonix' |
---|
279 | pixy = [73.242,73.242] |
---|
280 | File.seek(4096) |
---|
281 | if not imageOnly: |
---|
282 | print 'Read Rayonix MX300HE tiff file: ',filename |
---|
283 | image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
284 | # elif sizexy == [960,960]: |
---|
285 | # tiftype = 'PE-BE' |
---|
286 | # pixy = (200,200) |
---|
287 | # File.seek(8) |
---|
288 | # if not imageOnly: |
---|
289 | # print 'Read Gold tiff file:',filename |
---|
290 | # image = np.array(ar.array('H',File.read(2*Npix)),dtype=np.int32) |
---|
291 | |
---|
292 | if image is None: |
---|
293 | lines = ['not a known detector tiff file',] |
---|
294 | return lines,0,0,0 |
---|
295 | |
---|
296 | if sizexy[1]*sizexy[0] != image.size: # test is resize is allowed |
---|
297 | lines = ['not a known detector tiff file',] |
---|
298 | return lines,0,0,0 |
---|
299 | |
---|
300 | image = np.reshape(image,(sizexy[1],sizexy[0])) |
---|
301 | center = (not center[0]) and [pixy[0]*sizexy[0]/2000,pixy[1]*sizexy[1]/2000] or center |
---|
302 | wavelength = (not wavelength) and 0.10 or wavelength |
---|
303 | distance = (not distance) and 100.0 or distance |
---|
304 | data = {'pixelSize':pixy,'wavelength':wavelength,'distance':distance,'center':center,'size':sizexy} |
---|
305 | File.close() |
---|
306 | if imageOnly: |
---|
307 | return image |
---|
308 | else: |
---|
309 | return head,data,Npix,image |
---|