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

SCM Repository

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

Annotation of /trunk/src/compiler/nrrd/nrrd-info.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

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

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