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 1116 - (view) (download)
Original Path: trunk/src/compiler/fields/image-info.sml

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 165 datatype info = ImgInfo of {
18 : jhr 465 id : OS.FileSys.file_id, (* ID of image file *)
19 :     dim : int, (* dimension of space *)
20 : jhr 1116 ty : voxel_ty, (* types of image samples *)
21 : jhr 465 origin : FloatLit.float list, (* center of first sample *)
22 :     sizes : int list (* number of samples along each axis;
23 :     * we follow the Nrrd convention of
24 :     * listing the axes in fast to slow
25 :     * order.
26 :     *)
27 :     }
28 : jhr 165
29 :     (* are the underlying files the same? *)
30 :     val same : info * info -> bool
31 :    
32 :     (* hash value (based on image file ID) *)
33 :     val hash : info -> word
34 :    
35 :     (* get image info from a Nrrd file *)
36 : jhr 130 val getInfo : string -> info
37 : jhr 106
38 : jhr 192 val toString : info -> string
39 : jhr 1116 val dim : info -> int (* dimension of space *)
40 :     val sizes : info -> int list (* size of each dimension *)
41 :     val voxelShape : info -> int list (* shape of voxels; empty list for scalars *)
42 :     val voxelSzB : info -> int (* size in bytes of a voxel *)
43 :     val stride : info -> int (* for non-scalar images, this returns the *)
44 :     (* number of samples between voxels *)
45 :     val sampleTy : info -> RawTypes.ty (* representation type of samples *)
46 : jhr 192
47 : jhr 106 end = struct
48 :    
49 : jhr 284 (* Image samples are tensors of some raw representation type *)
50 : jhr 1116 type voxel_ty = (int list * RawTypes.ty)
51 : jhr 284
52 : jhr 165 datatype info = ImgInfo of {
53 : jhr 465 id : OS.FileSys.file_id, (* ID of image file *)
54 :     dim : int, (* dimension of space *)
55 : jhr 1116 ty : voxel_ty, (* types of image samples *)
56 : jhr 465 origin : FloatLit.float list, (* center of first sample *)
57 :     sizes : int list (* number of samples along each axis;
58 :     * we follow the Nrrd convention of
59 :     * listing the axes in fast to slow
60 :     * order.
61 :     *)
62 :     }
63 : jhr 135
64 : jhr 165 fun same (ImgInfo{id=a, ...}, ImgInfo{id=b, ...}) = (a = b)
65 :    
66 :     fun hash (ImgInfo{id, ...}) = OS.FileSys.hash id
67 :    
68 : jhr 1116 fun doHeader (fileName, header) = let
69 : jhr 511 fun fields s = String.tokens Char.isSpace s
70 : jhr 239 fun set (r, v) = (r := SOME v)
71 :     fun get (tag, r) = (case !r of NONE => raise Fail("missing "^tag) | SOME v => v)
72 :     val ty = ref NONE
73 : jhr 284 val totalDim = ref NONE
74 : jhr 239 val dim = ref NONE
75 : jhr 511 val sizes = ref NONE
76 : jhr 239 fun doValue ("type", v) = set(ty, RawTypes.fromString v)
77 : jhr 284 | doValue ("dimension", v) = set (totalDim, valOf(Int.fromString v))
78 : jhr 239 | doValue ("space dimension", v) = set (dim, valOf(Int.fromString v))
79 : jhr 511 | doValue ("sizes", v) = let
80 :     fun atoi s = (case Int.fromString s
81 :     of SOME i => i
82 :     | NONE => raise Fail(concat[
83 :     "bogus dnorm output: \"", "sizes: ", String.toString v, "\n"
84 :     ])
85 :     (* end case *))
86 :     in
87 :     set (sizes, List.map atoi (fields v))
88 :     end
89 : jhr 239 | doValue ("space directions", v) = ()
90 :     | doValue ("kinds", v) = ()
91 :     | doValue ("endian", v) = ()
92 :     | doValue ("encoding", v) = ()
93 :     | doValue ("space origin", v) = ()
94 :     | doValue _ = ()
95 : jhr 135 in
96 : jhr 238 Log.msg (concat[fileName, " file header:\n"]);
97 :     List.app (fn (tag, value) => Log.msg(concat[" ", tag, ": ", value, "\n"])) header;
98 : jhr 239 List.app doValue header;
99 : jhr 1116 let
100 :     val dim = get ("space dimension", dim)
101 :     val rngDim = get ("dimension", totalDim) - dim
102 :     (* split the sizes into those that control the shape of the voxels, which come first
103 :     * in the list, and those that span the image's dimensions.
104 :     *)
105 :     val (rngShape, sizes) = let
106 :     fun split (0, l, prefix) = (List.rev prefix, l)
107 :     | split (i, d::l, prefix) = split(i-1, l, d::prefix)
108 :     | split _ = raise Fail "bogus dnorm output: too few sizes"
109 :     in
110 :     split (rngDim, get ("sizes", sizes), [])
111 :     end
112 :     in
113 :     ImgInfo{
114 :     id = OS.FileSys.fileId fileName,
115 :     dim = dim,
116 :     ty = (rngShape, get ("type", ty)),
117 :     origin = [], (* FIXME *)
118 :     sizes = sizes
119 :     }
120 :     end
121 : jhr 135 end
122 :    
123 : jhr 1116 fun getInfo fileName = if OS.FileSys.access(fileName, [OS.FileSys.A_READ])
124 :     then let
125 :     val {version, header} = RunDNorm.run fileName
126 :     in
127 :     doHeader (fileName, header)
128 :     end
129 :     else raise Fail(concat["Nrrd file \"", fileName, "\" does not exist"])
130 : jhr 192
131 : jhr 1116 fun toString (ImgInfo{dim, ty=(dd, rTy), ...}) = let
132 :     val shape = (case dd
133 :     of [] => ""
134 :     | [d] => concat["{", Int.toString d, "}"]
135 :     | dd => concat["{", String.concatWith "," (List.map Int.toString dd), "}"]
136 :     (* end case *))
137 :     in
138 :     concat[
139 :     "IMAGE", Int.toString dim, "D<", RawTypes.toString rTy, shape, ">"
140 :     ]
141 :     end
142 :    
143 : jhr 394 fun dim (ImgInfo{dim, ...}) = dim
144 :    
145 : jhr 1116 fun sizes (ImgInfo{sizes, ...}) = sizes
146 :    
147 :     fun voxelShape (ImgInfo{ty=(dd, _), ...}) = dd
148 :    
149 :     fun voxelSzB (ImgInfo{ty=(dd, rTy), ...}) = let
150 :     val nSamples = List.foldl (op * ) 1 dd
151 :     in
152 :     nSamples * RawTypes.sizeb rTy
153 :     end
154 :    
155 :     fun stride (ImgInfo{ty=(dd, rTy), ...}) = List.foldl (op * ) 1 dd
156 :    
157 :     fun sampleTy (ImgInfo{ty=(_, rTy), ...}) = rTy
158 :    
159 : jhr 106 end

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