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

SCM Repository

[diderot] View of /branches/vis15/src/lib/include/diderot/image.hxx
ViewVC logotype

View of /branches/vis15/src/lib/include/diderot/image.hxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3824 - (download) (as text) (annotate)
Thu May 5 15:57:28 2016 UTC (3 years, 4 months ago) by jhr
File size: 7251 byte(s)
  working on merge: runtime
/*! \file image.hxx
 *
 * \author John Reppy
 */

/*
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2016 The University of Chicago
 * All rights reserved.
 */

#ifndef _IMAGE_HXX_
#define _IMAGE_HXX_

#include <memory>
#include <string>
#include <teem/nrrd.h>

namespace Diderot {

/* stuff that should go elsewhere eventually ! */
    template <typename REAL> using vec2 = REAL[2];
    template <typename REAL> using vec3 = REAL[3];

    template <typename REAL> using mat2x2 = REAL[4];
    template <typename REAL> using mat3x3 = REAL[9];
    template <typename REAL> using mat4x4 = REAL[16];

    void error (struct WorldBase *wrld, const char *string, ...);

    Nrrd *load_nrrd_file (struct WorldBase *wrld, std::string const &name);
/* end stuff that should go elsewhere eventually ! */

    namespace __details {

      //! clamp an index between 0 and size-1
	inline int index_clamp (int size, int idx)
	{
	    return (idx < 0) ? 0 : (idx < size) ? idx : (size-1);
	}

      // mirror an index if it is out of bounds
	inline int index_mirror (int size, int idx)
	{
	    if (idx < 0) {
		idx = -1 - idx;
	    }
	    if (idx < size)
		return idx;
	  // here size <= idx
	    int div = idx / size;
	    int rem = idx - size * div;
	    return (div & 1) ? (size - 1) - rem : rem;
	}

      // wrap an index if it is out of bounds
	inline int index_wrap (int size, int idx)
	{
	    if (idx < 0) {
		return (size - 1) + (idx + 1) % size;
	    }
	    else if (idx < size) {
		return idx;
	    }
	    else {
		return idx % size;
	    }
	}

	template <typename REAL>
	struct image1d {
	    uint32_t            _dim;           //!< dimension (== 1)
	    uint32_t            _size[1];
	    size_t              _dataSzb;       //!< size of data in bytes
	    void                *_data;
	    REAL                _s;             //!< scaling from world-space to image-space
	    REAL                _t;             //!< translation from world-space to image-space

	    image1d ();
	    ~image1d ();

	    bool load (struct WorldBase *wrld, const Nrrd *nin);
	};

	template <typename REAL>
	struct image2d {
	    uint32_t            _dim;           //!< dimension (== 2)
	    uint32_t            _size[2];       //!< sizes (fast to slow)
	    size_t              _dataSzb;       //!< size of data in bytes
	    void                *_data;
	    mat2x2<REAL>        _w2i;           //!< affine tranform from world space to index space.  This is the
						//!  inverse of the index to world-space transform that is loaded from
						//!  the Nrrd file.
	    vec2<REAL>          _tVec;          //!< translation part of world to index transform
	    mat2x2<REAL>        _w2iT;          //!< transpose w3i

	    image2d ();
	    ~image2d ();

	    bool load (struct WorldBase *wrld, const Nrrd *nin);
	};

	template <typename REAL>
	struct image3d {
	    uint32_t            _dim;           //!< dimension (== 3)
	    uint32_t            _size[3];       //!< sizes (fast to slow)
	    size_t              _dataSzb;       //!< size of data in bytes
	    void                *_data;
	    mat3x3<REAL>        _w2i;           //!< affine tranform from world space to index space.  This is the
						//!  inverse of the index to world-space transform that is loaded from
						//!  the Nrrd file.
	    vec3<REAL>          _tVec;          //!< translation part of world to index transform
	    mat3x3<REAL>        _w2iT;          //!< transpose w3i

	    image3d ();
	    ~image3d ();

	    bool load (struct WorldBase *wrld, const Nrrd *nin);
	};

    } // namespace __details

    template <typename REAL>
    class image1d {
      public:

	image1d () : _img(nullptr) { }
	~image1d () { }

	bool load (struct WorldBase *wrld, std::string const &name);
	bool load (struct WorldBase *wrld, const Nrrd *nin);

	bool inside (int idx, int s) const;
	int clamp (int dim, int idx) const;
	int mirror (int dim, int idx) const;
	int wrap (int dim, int idx) const;

      private:
	std::shared_ptr<__details::image1d<REAL>> _img;
    };

    template <typename REAL>
    class image2d {
      public:

	image2d () : _img(nullptr) { }
	~image2d () { }

	bool load (struct WorldBase *wrld, std::string const &name);
	bool load (struct WorldBase *wrld, const Nrrd *nin);

	bool inside (int idx[2], int s) const;
	int clamp (int dim, int idx) const;
	int mirror (int dim, int idx) const;
	int wrap (int dim, int idx) const;

      private:
	std::shared_ptr<__details::image2d<REAL>> _img;
    };

    template <typename REAL>
    class image3d {
      public:

	image3d () : _img(nullptr) { }
	~image3d () { }

	bool load (struct WorldBase *wrld, std::string const &name);
	bool load (struct WorldBase *wrld, const Nrrd *nin);

	bool inside (int idx[3], int s) const;
	int clamp (int dim, int idx) const;
	int mirror (int dim, int idx) const;
	int wrap (int dim, int idx) const;

      private:
	std::shared_ptr<__details::image3d<REAL>> _img;
    };

  /***** image1d inline functions *****/
    template <typename REAL>
    bool image1d<REAL>::inside (int idx, int s) const
    {
	return ((s-1 < idx) && (idx < (this->_img->_size[0] - s)));
    }

    template <typename REAL>
    int image1d<REAL>::clamp (int dim, int idx) const
    {
	return __details::index_clamp(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image1d<REAL>::mirror (int dim, int idx) const
    {
	return __details::index_mirror(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image1d<REAL>::wrap (int dim, int idx) const
    {
	return __details::index_wrap(this->_img->_size[dim], idx);
    }

  /***** image2d inline functions *****/
    template <typename REAL>
    bool image2d<REAL>::inside (int idx[2], int s) const
    {
	return ((s-1 < idx[0]) && (idx[0] < (this->_img->_size[0] - s))
	    &&  (s-1 < idx[1]) && (idx[1] < (this->_img->_size[1] - s)));
    }

    template <typename REAL>
    int image2d<REAL>::clamp (int dim, int idx) const
    {
	return __details::index_clamp(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image2d<REAL>::mirror (int dim, int idx) const
    {
	return __details::index_mirror(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image2d<REAL>::wrap (int dim, int idx) const
    {
	return __details::index_wrap(this->_img->_size[dim], idx);
    }

  /***** image2d inline functions *****/
    template <typename REAL>
    bool image3d<REAL>::inside (int idx[3], int s) const
    {
	return ((s-1 < idx[0]) && (idx[0] < (this->_img->_size[0] - s))
	    &&  (s-1 < idx[1]) && (idx[1] < (this->_img->_size[1] - s))
	    &&  (s-1 < idx[2]) && (idx[2] < (this->_img->_size[2] - s)));
    }

    template <typename REAL>
    int image3d<REAL>::clamp (int dim, int idx) const
    {
	return __details::index_clamp(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image3d<REAL>::mirror (int dim, int idx) const
    {
	return __details::index_mirror(this->_img->_size[dim], idx);
    }

    template <typename REAL>
    int image3d<REAL>::wrap (int dim, int idx) const
    {
	return __details::index_wrap(this->_img->_size[dim], idx);
    }

} // namespace Diderot

#endif //! _IMAGE_HXX_

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