Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/raw-data/image.sml
ViewVC logotype

View of /trunk/sml3d/src/raw-data/image.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 659 - (download) (annotate)
Sat Jan 2 04:50:37 2010 UTC (9 years, 9 months ago) by jhr
File size: 2289 byte(s)
  Factor out support for low-level C data into separate library.  This library
  is used by both the OpenCL and OpenGL libraries.
(* image.sml
 *
 * COPYRIGHT (c) 2010 John Reppy (http://cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * 1D, 2D, and 3D images built on data buffers.  For use by both OpenGL and OpenCL programs.
 *)

structure Image :> IMAGE = struct

    structure DB = DataBuffer

    type 'a buffer = 'a DB.buffer

    datatype 'a image1D = IMG1 of 'a buffer
    datatype 'a image2D	= IMG2 of {wid : int, ht : int, data : 'a buffer}
    datatype 'a image3D	= IMG3 of {wid : int, ht : int, depth : int, data : 'a buffer}

  (** check the range of an index *)
    fun checkRange (i, sz) = Word.<(Word.fromInt i, Word.fromInt sz)

  (** 1D images **)
    fun image1D {wid, data} =
	  if (DB.length data <> wid)
	    then raise Size
	    else IMG1 data

    fun width1D (IMG1 data) = DB.length data
    fun data1D (IMG1 data) = data
    fun get1D (IMG1 data, i) = DB.get(data, i)
    fun set1D (IMG1 data, i, v) = DB.set(data, i, v)

  (** 2D images **)
    fun image2D {wid, ht, data} =
	  if (DB.length data <> wid*ht)
	    then raise Size
	    else IMG2{wid=wid, ht=ht, data=data}

    fun width2D (IMG2{wid, ...}) = wid
    fun height2D (IMG2{ht, ...}) = ht
    fun data2D (IMG2{data, ...}) = data
    fun get2D (IMG2{data, wid, ht}, row, col) =
	  if checkRange(row, ht) andalso checkRange(col, wid)
	    then DB.get(data, row*wid + col)
	    else raise Subscript
    fun set2D (IMG2{data, wid, ht}, row, col, v) =
	  if checkRange(row, ht) andalso checkRange(col, wid)
	    then DB.set(data, row*wid + col, v)
	    else raise Subscript

  (** 3D images **)
    fun image3D {wid, ht, depth, data} =
	  if (DB.length data <> wid*ht*depth)
	    then raise Size
	    else IMG3{wid=wid, ht=ht, depth=depth, data=data}

    fun width3D (IMG3{wid, ...}) = wid
    fun height3D (IMG3{ht, ...}) = ht
    fun depth3D (IMG3{depth, ...}) = depth
    fun data3D (IMG3{data, ...}) = data
    fun get3D (IMG3{data, wid, ht, depth}, d, r, c) =
	  if checkRange(d, depth) andalso checkRange(r, ht) andalso checkRange(c, wid)
	    then DB.get(data, d*wid*ht + r*wid + c)
	    else raise Subscript
    fun set3D (IMG3{data, wid, ht, depth}, d, r, c, v) =
	  if checkRange(d, depth) andalso checkRange(r, ht) andalso checkRange(c, wid)
	    then DB.set(data, d*wid*ht + r*wid + c, v)
	    else raise Subscript

  end

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