53
54
def isSpiderHeader(t):
54
55
h = (99,) + t # add 1 value so can use spider header index start=1
55
56
# header values 1,2,5,12,13,22,23 should be integers
56
if not isInt(h[1]): return 0
57
if not isInt(h[2]): return 0
58
if not isInt(h[5]): return 0
59
if not isInt(h[12]): return 0
60
if not isInt(h[13]): return 0
61
if not isInt(h[22]): return 0
62
if not isInt(h[23]): return 0
57
for i in [1,2,5,12,13,22,23]:
58
if not isInt(h[i]): return 0
65
61
if not iform in iforms: return 0
96
n = 23 * 4 # read 23 float values
92
n = 27 * 4 # read 27 float values
97
93
f = self.fp.read(n)
100
96
self.bigendian = 1
101
t = struct.unpack('>23f',f) # try big-endian first
97
t = struct.unpack('>27f',f) # try big-endian first
102
98
hdrlen = isSpiderHeader(t)
104
100
self.bigendian = 0
105
t = struct.unpack('<23f',f) # little-endian
101
t = struct.unpack('<27f',f) # little-endian
106
102
hdrlen = isSpiderHeader(t)
108
104
raise SyntaxError, "not a valid Spider file"
109
105
except struct.error:
110
106
raise SyntaxError, "not a valid Spider file"
112
# size in pixels (width, height)
113
h = (99,) + t # add 1 value cos' spider header index starts at 1
108
h = (99,) + t # add 1 value : spider header index starts at 1
114
109
iform = int(h[5])
116
111
raise SyntaxError, "not a Spider 2D image"
118
self.size = int(h[12]), int(h[2])
113
self.size = int(h[12]), int(h[2]) # size in pixels (width, height)
114
self.istack = int(h[24])
115
self.imgnumber = int(h[27])
117
if self.istack == 0 and self.imgnumber == 0:
118
# stk=0, img=0: a regular 2D image
121
elif self.istack > 0 and self.imgnumber == 0:
122
# stk>0, img=0: Opening the stack for the first time
123
self.imgbytes = int(h[12]) * int(h[2]) * 4
125
self.nimages = int(h[26])
126
# Point to the first image in the stack
129
elif self.istack == 0 and self.imgnumber > 0:
130
# stk=0, img>0: an image within the stack
131
offset = hdrlen + self.stkoffset
132
self.istack = 2 # So Image knows it's still a stack
134
raise SyntaxError, "inconsistent stack header values"
120
136
if self.bigendian:
121
137
self.rawmode = "F;32BF"
123
139
self.rawmode = "F;32F"
126
self.tile = [("raw", (0, 0) + self.size, hdrlen,
142
self.tile = [("raw", (0, 0) + self.size, offset,
127
143
(self.rawmode, 0, 1))]
144
self.__fp = self.fp # FIXME: hack
146
# 1st image index is zero (although SPIDER imgnumber starts at 1)
148
if self.imgnumber < 1:
151
return self.imgnumber - 1
153
def seek(self, frame):
156
if frame >= self.nimages:
157
raise EOFError, "attempt to seek past end of file"
158
self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
160
self.fp.seek(self.stkoffset)
129
163
# returns a byte image after rescaling to 0..255
130
164
def convert2byte(self, depth=255):