1
2
3
4 """
5
6 Author: Andy Hammersley, ESRF
7 Translation into python/fabio: Jon Wright, ESRF
8 """
9
10 import numpy
11
12 from fabioimage import fabioimage
13
14
16 """ Read and try to write Andy Hammersley's mask format """
17
18
20 """
21 Read in a header from an already open file
22 """
23
24 header = infile.read(1024)
25 for i, j in [ ("M", 0),
26 ("A", 4),
27 ("S", 8),
28 ("K", 12) ]:
29 if header[j] != i:
30 raise Exception("Not a fit2d mask file")
31 fit2dhdr = numpy.fromstring(header, numpy.int32)
32 self.dim1 = fit2dhdr[4]
33 self.dim2 = fit2dhdr[5]
34
35
36 - def read(self, fname):
37 """
38 Read in header into self.header and
39 the data into self.data
40 """
41 fin = self._open(fname)
42 self._readheader(fin)
43
44 self.bytecode = numpy.uint8
45 self.bpp = len(numpy.array(0, self.bytecode).tostring())
46
47
48 num_ints = (self.dim1 + 31) // 32
49 total = self.dim2 * num_ints * 4
50 data = fin.read(total)
51 assert len(data) == total
52 fin.close()
53
54
55 data = numpy.fromstring(data, numpy.uint8)
56 data = numpy.reshape(data, (self.dim2, num_ints * 4))
57
58 result = numpy.zeros((self.dim2, num_ints * 4 * 8), numpy.uint8)
59
60
61 bits = numpy.ones((1), numpy.uint8)
62 for i in range(8):
63 temp = numpy.bitwise_and(bits, data)
64 result[:, i::8] = temp.astype(numpy.uint8)
65 bits = bits * 2
66
67 spares = num_ints * 4 * 8 - self.dim1
68 if spares == 0:
69 self.data = numpy.where(result == 0, 0, 1)
70 else:
71 self.data = numpy.where(result[:, :-spares] == 0, 0, 1)
72
73
74 self.data = numpy.reshape(self.data.astype(numpy.uint16),
75 (self.dim2, self.dim1))
76 self.pilimage = None
77
78
79
81 """
82 Try to write a file
83 check we can write zipped also
84 mimics that fabian was writing uint16 (we sometimes want floats)
85 """
86 raise Exception("Not implemented yet")
87