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 3607 - (view) (download)

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

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