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

SCM Repository

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

Annotation of /branches/vis12/src/compiler/fields/image-info.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1784 - (view) (download)

1 : jhr 106 (* image-info.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 106 * All rights reserved.
5 :     *
6 : jhr 1116 * Information about a NRRD file.
7 :     *
8 :     * TODO:
9 :     * handle images where the symmetries are exploited.
10 : jhr 106 *)
11 :    
12 :     structure ImageInfo : sig
13 :    
14 : jhr 1116 (* Image voxels are tensors of some raw representation type *)
15 :     type voxel_ty = (int list * RawTypes.ty)
16 : jhr 284
17 : jhr 1784 (* a subset of the Nrrd file axis kinds (see http://teem.sourceforge.net/nrrd/format.html#kinds) *)
18 :     datatype axis_kind
19 :     = SPACE
20 :     | SCALAR
21 :     | VEC of int (* 2, 3, or 4-vector *)
22 :     | MAT of int (* 2x2 or 3x3 matrix *)
23 :    
24 : jhr 165 datatype info = ImgInfo of {
25 : jhr 465 id : OS.FileSys.file_id, (* ID of image file *)
26 :     dim : int, (* dimension of space *)
27 : jhr 1116 ty : voxel_ty, (* types of image samples *)
28 : jhr 465 origin : FloatLit.float list, (* center of first sample *)
29 : jhr 1784 kinds : axis_kind list, (* the kinds of each axis;
30 : jhr 465 * we follow the Nrrd convention of
31 :     * listing the axes in fast to slow
32 :     * order.
33 :     *)
34 : jhr 1784 sizes : int list (* number of samples along each axis *)
35 : jhr 465 }
36 : jhr 165
37 :     (* are the underlying files the same? *)
38 :     val same : info * info -> bool
39 :    
40 :     (* hash value (based on image file ID) *)
41 :     val hash : info -> word
42 :    
43 :     (* get image info from a Nrrd file *)
44 : jhr 130 val getInfo : string -> info
45 : jhr 106
46 : jhr 192 val toString : info -> string
47 : jhr 1116 val dim : info -> int (* dimension of space *)
48 :     val sizes : info -> int list (* size of each dimension *)
49 :     val voxelShape : info -> int list (* shape of voxels; empty list for scalars *)
50 :     val voxelSzB : info -> int (* size in bytes of a voxel *)
51 :     val stride : info -> int (* for non-scalar images, this returns the *)
52 :     (* number of samples between voxels *)
53 :     val sampleTy : info -> RawTypes.ty (* representation type of samples *)
54 : jhr 192
55 : jhr 106 end = struct
56 :    
57 : jhr 284 (* Image samples are tensors of some raw representation type *)
58 : jhr 1116 type voxel_ty = (int list * RawTypes.ty)
59 : jhr 284
60 : jhr 1784 (* a subset of the Nrrd file axis kinds (see http://teem.sourceforge.net/nrrd/format.html#kinds) *)
61 :     datatype axis_kind
62 :     = SPACE
63 :     | SCALAR
64 :     | VEC of int (* 2, 3, or 4-vector *)
65 :     | MAT of int (* 2x2 or 3x3 matrix *)
66 :    
67 : jhr 165 datatype info = ImgInfo of {
68 : jhr 465 id : OS.FileSys.file_id, (* ID of image file *)
69 :     dim : int, (* dimension of space *)
70 : jhr 1116 ty : voxel_ty, (* types of image samples *)
71 : jhr 465 origin : FloatLit.float list, (* center of first sample *)
72 : jhr 1784 kinds : axis_kind list, (* the kinds of each axis;
73 : jhr 465 * we follow the Nrrd convention of
74 :     * listing the axes in fast to slow
75 :     * order.
76 :     *)
77 : jhr 1784 sizes : int list (* number of samples along each axis *)
78 : jhr 465 }
79 : jhr 135
80 : jhr 165 fun same (ImgInfo{id=a, ...}, ImgInfo{id=b, ...}) = (a = b)
81 :    
82 :     fun hash (ImgInfo{id, ...}) = OS.FileSys.hash id
83 :    
84 : jhr 1116 fun doHeader (fileName, header) = let
85 : jhr 511 fun fields s = String.tokens Char.isSpace s
86 : jhr 239 fun set (r, v) = (r := SOME v)
87 :     fun get (tag, r) = (case !r of NONE => raise Fail("missing "^tag) | SOME v => v)
88 :     val ty = ref NONE
89 : jhr 284 val totalDim = ref NONE
90 : jhr 239 val dim = ref NONE
91 : jhr 1784 val kinds = ref NONE
92 : jhr 511 val sizes = ref NONE
93 : jhr 239 fun doValue ("type", v) = set(ty, RawTypes.fromString v)
94 : jhr 284 | doValue ("dimension", v) = set (totalDim, valOf(Int.fromString v))
95 : jhr 239 | doValue ("space dimension", v) = set (dim, valOf(Int.fromString v))
96 : jhr 511 | doValue ("sizes", v) = let
97 :     fun atoi s = (case Int.fromString s
98 :     of SOME i => i
99 :     | NONE => raise Fail(concat[
100 :     "bogus dnorm output: \"", "sizes: ", String.toString v, "\n"
101 :     ])
102 :     (* end case *))
103 :     in
104 :     set (sizes, List.map atoi (fields v))
105 :     end
106 : jhr 239 | doValue ("space directions", v) = ()
107 : jhr 1784 | doValue ("kinds", v) = let
108 :     fun s2kind "space" = SPACE
109 :     | s2kind "scalar" = SCALAR
110 :     | s2kind "2-vector" = VEC 2
111 :     | s2kind "3-vector" = VEC 3
112 :     | s2kind "4-vector" = VEC 4
113 :     | s2kind "2D-matrix" = MAT 2
114 :     | s2kind "3D-matrix" = MAT 3
115 :     | s2kind k = raise Fail(concat["axis kind \"", k, "\" not supported"])
116 :     in
117 :     set (kinds, List.map s2kind (fields v))
118 :     end
119 : jhr 239 | doValue ("endian", v) = ()
120 :     | doValue ("encoding", v) = ()
121 :     | doValue ("space origin", v) = ()
122 :     | doValue _ = ()
123 : jhr 135 in
124 : jhr 238 Log.msg (concat[fileName, " file header:\n"]);
125 :     List.app (fn (tag, value) => Log.msg(concat[" ", tag, ": ", value, "\n"])) header;
126 : jhr 239 List.app doValue header;
127 : jhr 1116 let
128 :     val dim = get ("space dimension", dim)
129 :     val rngDim = get ("dimension", totalDim) - dim
130 :     (* split the sizes into those that control the shape of the voxels, which come first
131 :     * in the list, and those that span the image's dimensions.
132 :     *)
133 :     val (rngShape, sizes) = let
134 :     fun split (0, l, prefix) = (List.rev prefix, l)
135 :     | split (i, d::l, prefix) = split(i-1, l, d::prefix)
136 :     | split _ = raise Fail "bogus dnorm output: too few sizes"
137 :     in
138 :     split (rngDim, get ("sizes", sizes), [])
139 :     end
140 :     in
141 :     ImgInfo{
142 :     id = OS.FileSys.fileId fileName,
143 :     dim = dim,
144 :     ty = (rngShape, get ("type", ty)),
145 :     origin = [], (* FIXME *)
146 : jhr 1784 kinds = get ("kinds", kinds),
147 : jhr 1116 sizes = sizes
148 :     }
149 :     end
150 : jhr 135 end
151 :    
152 : jhr 1116 fun getInfo fileName = if OS.FileSys.access(fileName, [OS.FileSys.A_READ])
153 :     then let
154 :     val {version, header} = RunDNorm.run fileName
155 :     in
156 :     doHeader (fileName, header)
157 :     end
158 :     else raise Fail(concat["Nrrd file \"", fileName, "\" does not exist"])
159 : jhr 192
160 : jhr 1116 fun toString (ImgInfo{dim, ty=(dd, rTy), ...}) = let
161 :     val shape = (case dd
162 :     of [] => ""
163 :     | [d] => concat["{", Int.toString d, "}"]
164 :     | dd => concat["{", String.concatWith "," (List.map Int.toString dd), "}"]
165 :     (* end case *))
166 :     in
167 :     concat[
168 :     "IMAGE", Int.toString dim, "D<", RawTypes.toString rTy, shape, ">"
169 :     ]
170 :     end
171 :    
172 : jhr 394 fun dim (ImgInfo{dim, ...}) = dim
173 :    
174 : jhr 1116 fun sizes (ImgInfo{sizes, ...}) = sizes
175 :    
176 :     fun voxelShape (ImgInfo{ty=(dd, _), ...}) = dd
177 :    
178 :     fun voxelSzB (ImgInfo{ty=(dd, rTy), ...}) = let
179 :     val nSamples = List.foldl (op * ) 1 dd
180 :     in
181 :     nSamples * RawTypes.sizeb rTy
182 :     end
183 :    
184 :     fun stride (ImgInfo{ty=(dd, rTy), ...}) = List.foldl (op * ) 1 dd
185 :    
186 :     fun sampleTy (ImgInfo{ty=(_, rTy), ...}) = rTy
187 :    
188 : jhr 106 end

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