1 """
2
3 Authors: Henning O. Sorensen & Erik Knudsen
4 Center for Fundamental Research: Metal Structures in Four Dimensions
5 Risoe National Laboratory
6 Frederiksborgvej 399
7 DK-4000 Roskilde
8 email:henning.sorensen@risoe.dk
9
10 mods for fabio by JPW
11
12 """
13 import sys, logging
14 logger = logging.getLogger("openimage")
15 from fabioutils import FilenameObject
16 from fabioimage import fabioimage
17 import edfimage
18 import adscimage
19 import tifimage
20 import marccdimage
21 import mar345image
22 import fit2dmaskimage
23 import brukerimage
24 import bruker100image
25 import pnmimage
26 import GEimage
27 import OXDimage
28 import dm3image
29 import HiPiCimage
30 import pilatusimage
31 import fit2dspreadsheetimage
32 import kcdimage
33 import cbfimage
34 import xsdimage
35 import binaryimage
36
37 MAGIC_NUMBERS = [
38
39
40 ("FORMAT : 86" , 'bruker'),
41 ("\x4d\x4d\x00\x2a" , 'tif') ,
42
43
44
45 ("\x49\x49\x2a\x00\x08\x00" , 'marccd') ,
46 ("\x49\x49\x2a\x00\x82\x00" , 'pilatus') ,
47 ("\x49\x49\x2a\x00" , 'tif') ,
48
49 ("{\nHEA" , 'adsc'),
50 ("{" , 'edf'),
51 ("\r{" , 'edf'),
52 ("\n{" , 'edf'),
53 ("ADEPT" , 'GE'),
54 ("OD" , 'OXD'),
55 ("IM" , 'HiPiC'),
56 ('\x2d\x04' , 'mar345'),
57 ('\xd2\x04' , 'mar345'),
58 ('\x04\x2d' , 'mar345'),
59 ('\x04\xd2' , 'mar345'),
60
61 ('M\x00\x00\x00A\x00\x00\x00S\x00\x00\x00K\x00\x00\x00' , 'fit2dmask') ,
62 ('\x00\x00\x00\x03' , 'dm3'),
63 ("No" , "kcd"),
64 ("<" , "xsd")
65 ]
66
68 """ Try to interpret the bytes starting the file as a magic number """
69 for magic, format_type in MAGIC_NUMBERS:
70 if byts.find(magic) == 0:
71 return format_type
72 if 0:
73 logger.debug("m: %s f: %s", magic, format_type)
74 logger.debug("bytes: %s len(bytes) %s", magic, len(magic))
75 logger.debug("found: %s", byts.find(magic))
76 for i in range(len(magic)):
77 logger.debug("%s %s %s %s ", ord(magic[i]), ord(byts[i]), magic[i], byts[i])
78 raise Exception("Could not interpret magic string")
79
80
82 """ Try to open an image """
83 if isinstance(filename, FilenameObject):
84 try:
85 logger.debug("Attempting to open %s" % (filename.tostring()))
86 obj = _openimage(filename.tostring())
87 logger.debug("Attempting to read frame %s from %s" % (frame,
88 filename.tostring()))
89 obj = obj.read(filename.tostring(), frame)
90 except Exception, ex:
91
92
93
94 logger.debug("Exception %s, trying name %s" % (ex, filename.stem))
95 obj = _openimage(filename.stem)
96 logger.debug("Reading frame %s from %s" % (filename.num, filename.stem))
97 obj.read(filename.stem, frame=filename.num)
98 else:
99 logger.debug("Attempting to open %s" % (filename))
100 obj = _openimage(filename)
101 logger.debug("Attempting to read frame %s from %s" % (frame, filename))
102 obj = obj.read(filename, frame)
103 return obj
104
105
107 """ return only the header"""
108 obj = _openimage(filename)
109 obj.readheader(filename)
110 return obj
111
112
114 """
115 determine which format for a filename
116 and return appropriate class which can be used for opening the image
117 """
118 try:
119 imo = fabioimage()
120 byts = imo._open(filename).read(18)
121 filetype = do_magic(byts)
122 if filetype == "marccd" and filename.find("mccd") == -1:
123
124
125 filetype = "tif"
126 except IOError, error:
127 logger.error("%s: File probably does not exist", error)
128 raise error
129 except:
130 try:
131 file_obj = FilenameObject(filename=filename)
132 if file_obj == None:
133 raise Exception("Unable to deconstruct filename")
134 if (file_obj.format is not None) and\
135 len(file_obj.format) != 1 and \
136 type(file_obj.format) != type(["list"]):
137
138 raise Exception("openimage failed on magic bytes & name guess")
139 filetype = file_obj.format
140
141 except Exception, error:
142 logger.error(error)
143 import traceback
144 traceback.print_exc()
145 raise Exception("Fabio could not identify " + filename)
146 klass_name = "".join(filetype) + 'image'
147 module = sys.modules.get("fabio." + klass_name, None)
148 if module is not None:
149 if hasattr(module, klass_name):
150 klass = getattr(module, klass_name)
151 else:
152 raise Exception("Module %s has no image class" % module)
153 else:
154 raise Exception("Filetype not known %s %s" % (filename, klass_name))
155 obj = klass()
156
157 return obj
158