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

SCM Repository

[diderot] Diff of /trunk/src/compiler/fields/image-info.sml
ViewVC logotype

Diff of /trunk/src/compiler/fields/image-info.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1115, Thu May 5 04:42:18 2011 UTC revision 1116, Thu May 5 04:49:02 2011 UTC
# Line 3  Line 3 
3   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5   *   *
6   * Information about a NRRD file   * Information about a NRRD file.
7     *
8     * TODO:
9     *      handle images where the symmetries are exploited.
10   *)   *)
11    
12  structure ImageInfo : sig  structure ImageInfo : sig
13    
14    (* Image samples are tensors of some raw representation type *)    (* Image voxels are tensors of some raw representation type *)
15      type sample_ty = (int list * RawTypes.ty)      type voxel_ty = (int list * RawTypes.ty)
16    
17      datatype info = ImgInfo of {      datatype info = ImgInfo of {
18          id : OS.FileSys.file_id,        (* ID of image file *)          id : OS.FileSys.file_id,        (* ID of image file *)
19          dim : int,                      (* dimension of space *)          dim : int,                      (* dimension of space *)
20          ty : sample_ty,                 (* types of image samples *)          ty : voxel_ty,                  (* types of image samples *)
21          origin : FloatLit.float list,   (* center of first sample *)          origin : FloatLit.float list,   (* center of first sample *)
22          sizes : int list                (* number of samples along each axis;          sizes : int list                (* number of samples along each axis;
23                                           * we follow the Nrrd convention of                                           * we follow the Nrrd convention of
# Line 34  Line 37 
37    
38      val toString : info -> string      val toString : info -> string
39      val dim : info -> int                       (* dimension of space *)      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    
47    end = struct    end = struct
48    
49    (* Image samples are tensors of some raw representation type *)    (* Image samples are tensors of some raw representation type *)
50      type sample_ty = (int list * RawTypes.ty)      type voxel_ty = (int list * RawTypes.ty)
51    
52      datatype info = ImgInfo of {      datatype info = ImgInfo of {
53          id : OS.FileSys.file_id,        (* ID of image file *)          id : OS.FileSys.file_id,        (* ID of image file *)
54          dim : int,                      (* dimension of space *)          dim : int,                      (* dimension of space *)
55          ty : sample_ty,                 (* types of image samples *)          ty : voxel_ty,                  (* types of image samples *)
56          origin : FloatLit.float list,   (* center of first sample *)          origin : FloatLit.float list,   (* center of first sample *)
57          sizes : int list                (* number of samples along each axis;          sizes : int list                (* number of samples along each axis;
58                                           * we follow the Nrrd convention of                                           * we follow the Nrrd convention of
# Line 56  Line 65 
65    
66      fun hash (ImgInfo{id, ...}) = OS.FileSys.hash id      fun hash (ImgInfo{id, ...}) = OS.FileSys.hash id
67    
68      fun getInfo fileName = let      fun doHeader (fileName, header) = let
 (* FIXME: check that file exists! *)  
           val {version, header} = RunDNorm.run fileName  
69            fun fields s = String.tokens Char.isSpace s            fun fields s = String.tokens Char.isSpace s
70            fun set (r, v) = (r := SOME v)            fun set (r, v) = (r := SOME v)
71            fun get (tag, r) = (case !r of NONE => raise Fail("missing "^tag) | SOME v => v)            fun get (tag, r) = (case !r of NONE => raise Fail("missing "^tag) | SOME v => v)
# Line 89  Line 96 
96              Log.msg (concat[fileName, " file header:\n"]);              Log.msg (concat[fileName, " file header:\n"]);
97              List.app (fn (tag, value) => Log.msg(concat["  ", tag, ": ", value, "\n"])) header;              List.app (fn (tag, value) => Log.msg(concat["  ", tag, ": ", value, "\n"])) header;
98              List.app doValue header;              List.app doValue header;
99                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{              ImgInfo{
114                  id = OS.FileSys.fileId fileName,                  id = OS.FileSys.fileId fileName,
115                  dim = get ("space dimension", dim),                    dim = dim,
116  (* FIXME: in general, the type is a tensor of raw values *)                    ty = (rngShape, get ("type", ty)),
                 ty = ([], get ("type", ty)),  
117                  origin = [],    (* FIXME *)                  origin = [],    (* FIXME *)
118                  sizes = get ("sizes", sizes)                    sizes = sizes
119                }                }
120            end            end
121              end
122    
123        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    
131      fun toString (ImgInfo{dim, ...}) = concat["IMAGE", Int.toString dim, "D"] (* FIXME *)      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      fun dim (ImgInfo{dim, ...}) = dim      fun dim (ImgInfo{dim, ...}) = dim
144    
145        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    end    end

Legend:
Removed from v.1115  
changed lines
  Added in v.1116

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