Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/nrrd/nrrd-info.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/nrrd/nrrd-info.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1997 - (view) (download)

1 : jhr 1793 (* nrrd-info.sml
2 :     *
3 :     * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Information about a NRRD file.
7 :     *
8 :     * TODO:
9 :     * handle files where the symmetries are exploited.
10 :     *)
11 :    
12 :     structure NrrdInfo : sig
13 :    
14 :     datatype info = NrrdInfo of {
15 :     id : OS.FileSys.file_id, (* ID of nrrd file *)
16 :     dim : int, (* dimension of space *)
17 :     ty : RawTypes.ty, (* types of data values *)
18 :     origin : FloatLit.float list, (* center of first sample *)
19 :     dataKind : NrrdEnums.axis_kind, (* the nrrd axis kind of the data axis *)
20 :     nElems : int, (* the number of elements per voxel *)
21 :     sizes : int list (* number of samples along each axis (not including
22 :     * the data axis); we follow the Nrrd convention of
23 :     * listing the axes in fast to slow order.
24 :     *)
25 :     }
26 :    
27 :     (* are the underlying files the same? *)
28 :     val same : info * info -> bool
29 :    
30 :     (* hash value (based on nrrd file ID) *)
31 :     val hash : info -> word
32 :    
33 :     (* get info from a Nrrd file *)
34 :     val getInfo : string -> info
35 :    
36 :     val dim : info -> int (* dimension of space *)
37 :     val sizes : info -> int list (* size of each dimension (not including the data axis) *)
38 :     val voxelInfo : info -> { (* get information about the data in the file *)
39 :     elemTy : RawTypes.ty, (* type of voxel elements *)
40 :     kind : NrrdEnums.axis_kind, (* nrrd file kind of voxel *)
41 :     nElems : int (* number of elements per voxel *)
42 :     }
43 :     val voxelSzB : info -> int (* size in bytes of a voxel *)
44 :     val stride : info -> int (* for non-scalar data, this returns the *)
45 :     (* number of samples between voxels *)
46 :     val sampleTy : info -> RawTypes.ty (* representation type of samples *)
47 :    
48 :     end = struct
49 :    
50 :     datatype info = NrrdInfo of {
51 :     id : OS.FileSys.file_id, (* ID of nrrd file *)
52 :     dim : int, (* dimension of space *)
53 :     ty : RawTypes.ty, (* the type of the elements *)
54 :     origin : FloatLit.float list, (* center of first sample *)
55 :     dataKind : NrrdEnums.axis_kind, (* the nrrd axis kind of the data axis *)
56 :     nElems : int, (* the number of elements per voxel *)
57 :     sizes : int list (* number of samples along each axis (not including
58 :     * the data axis); we follow the Nrrd convention of
59 :     * listing the axes in fast to slow order.
60 :     *)
61 :     }
62 :    
63 :     fun same (NrrdInfo{id=a, ...}, NrrdInfo{id=b, ...}) = (a = b)
64 :    
65 :     fun hash (NrrdInfo{id, ...}) = OS.FileSys.hash id
66 :    
67 :     fun doHeader (fileName, header) = let
68 :     fun fields s = String.tokens Char.isSpace s
69 :     fun set (r, v) = (r := SOME v)
70 :     fun get (tag, r) = (case !r of NONE => raise Fail("missing "^tag) | SOME v => v)
71 :     val ty = ref NONE
72 :     val totalDim = ref NONE
73 :     val dim = ref NONE
74 :     val kinds = ref NONE
75 :     val sizes = ref NONE
76 :     fun doValue ("type", v) = (case NrrdEnums.tyFromString v
77 :     of SOME ty' => set(ty, ty')
78 :     | NONE => raise Fail(concat[
79 :     "bogus dnorm output: \"", "type: ", String.toString v, "\n"
80 :     ])
81 :     (* end case *))
82 :     | doValue ("dimension", v) = set (totalDim, valOf(Int.fromString v))
83 :     | doValue ("space dimension", v) = set (dim, valOf(Int.fromString v))
84 :     | doValue ("sizes", v) = let
85 :     fun atoi s = (case Int.fromString s
86 :     of SOME i => i
87 :     | NONE => raise Fail(concat[
88 :     "bogus dnorm output: \"", "sizes: ", String.toString v, "\n"
89 :     ])
90 :     (* end case *))
91 :     in
92 :     set (sizes, List.map atoi (fields v))
93 :     end
94 :     | doValue ("space directions", v) = ()
95 :     | doValue ("kinds", v) = let
96 :     fun s2kind s = (case NrrdEnums.kindFromString s
97 :     of SOME k => k
98 :     | NONE => raise Fail(concat["axis kind \"", s, "\" not supported"])
99 :     (* end case *))
100 :     in
101 :     set (kinds, List.map s2kind (fields v))
102 :     end
103 :     | doValue ("endian", v) = ()
104 :     | doValue ("encoding", v) = ()
105 :     | doValue ("space origin", v) = ()
106 :     | doValue _ = ()
107 :     in
108 :     Log.msg (concat[fileName, " file header:\n"]);
109 :     List.app (fn (tag, value) => Log.msg(concat[" ", tag, ": ", value, "\n"])) header;
110 :     List.app doValue header;
111 :     let
112 :     val dim = get ("space dimension", dim)
113 :     val totalDim = get ("dimension", totalDim)
114 :     (* split the sizes into the data axis size, which comes first
115 :     * in the list, and those that span the space's dimensions. Note
116 :     * that for some scalar images, there is no data axis.
117 :     *)
118 :     val (nElems, sizes, dataKind) = if (dim = totalDim)
119 :     then (1, get ("sizes", sizes), NrrdEnums.KindScalar)
120 :     else let
121 :     val (nElems::sizes) = get ("sizes", sizes)
122 :     in
123 :     (nElems, sizes, hd (get ("kinds", kinds)))
124 :     end
125 :     in
126 :     NrrdInfo{
127 :     id = OS.FileSys.fileId fileName,
128 :     dim = dim,
129 :     ty = NrrdEnums.tyToRaw (get ("type", ty)),
130 :     origin = [], (* FIXME *)
131 :     dataKind = dataKind,
132 :     nElems = nElems,
133 :     sizes = sizes
134 :     }
135 :     end
136 :     end
137 :    
138 :     fun getInfo fileName = if OS.FileSys.access(fileName, [OS.FileSys.A_READ])
139 :     then let
140 :     val {version, header} = RunDNorm.run fileName
141 :     in
142 :     doHeader (fileName, header)
143 :     end
144 :     else raise Fail(concat["Nrrd file \"", fileName, "\" does not exist"])
145 :    
146 :     fun dim (NrrdInfo{dim, ...}) = dim
147 :    
148 :     fun sizes (NrrdInfo{sizes, ...}) = sizes
149 :    
150 :     (* get information about the data in the nrrd file *)
151 :     fun voxelInfo (NrrdInfo{ty, dataKind, nElems, ...}) =
152 :     {elemTy = ty, kind = dataKind, nElems = nElems}
153 :    
154 :     fun voxelSzB (NrrdInfo{ty, nElems, ...}) = nElems * RawTypes.sizeb ty
155 :    
156 :     fun stride (NrrdInfo{nElems, ...}) = nElems
157 :    
158 :     fun sampleTy (NrrdInfo{ty, ...}) = ty
159 :    
160 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0