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 3937 - (download) (as text) (annotate)
Mon Jun 6 13:16:10 2016 UTC (2 years, 11 months ago) by jhr
File size: 8914 byte(s)
  Working on merge: FIXMEs
/*! \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 _DIDEROT_IMAGE_HXX_
#define _DIDEROT_IMAGE_HXX_

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

#ifndef _DIDEROT_BASE_HXX_
#include "base.hxx"
#endif
#ifndef _DIDEROT_WORLD_HXX_
#include "world.hxx"
#endif

namespace diderot {

    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
	    REAL                *_data;
	    REAL                _s;             //!< scaling from world-space to image-space
	    REAL                _t;             //!< translation from world-space to image-space

	    image1d ();
	    ~image1d ();

	    bool load (struct world_base *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
	    REAL                *_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.
	    real2<REAL>          _tVec;         //!< translation part of world to index transform
	    mat2x2<REAL>        _w2iT;          //!< transpose w2i

	    image2d ();
	    ~image2d ();

	    bool load (struct world_base *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
	    REAL                *_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.
	    real3<REAL>          _tVec;         //!< translation part of world to index transform
	    mat3x3<REAL>        _w2iT;          //!< transpose w2i

	    image3d ();
	    ~image3d ();

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

    } // namespace __details

    template <typename REAL>
    class image1d {
      public:

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

	bool load (struct world_base *wrld, std::string const &name);
	bool load (struct world_base *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;

	uint32_t size (uint32_t dim) const;
	const REAL *base_addr () const;
	REAL operator[] (uint32_t idx) const;
	REAL world2image () const;
	REAL translate () const;

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

    template <typename REAL>
    class image2d {
      public:

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

	bool load (struct world_base *wrld, std::string const &name);
	bool load (struct world_base *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;

	uint32_t size (uint32_t dim) const;
	const REAL *base_addr () const;
	REAL operator[] (uint32_t idx) const;
	mat2x2<REAL> const &world2image () const;
	real2<REAL> const &translate () const;

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

    template <typename REAL>
    class image3d {
      public:

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

	bool load (struct world_base *wrld, std::string const &name);
	bool load (struct world_base *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;

	uint32_t size (uint32_t dim) const;
	const REAL *base_addr () const;
	REAL operator[] (uint32_t idx) const;
	mat3x3<REAL> const &world2image () const;
	real3<REAL> const &translate () 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);
    }

    template <typename REAL>
    uint32_t image1d<REAL>::size (uint32_t dim) const
    {
	return this->_img->_size;
    }

    template <typename REAL>
    const REAL *image1d<REAL>::base_addr () const
    {
	return this->_img->_data;
    }

    template <typename REAL>
    REAL image1d<REAL>::world2image () const
    {
	return this->_img->_s;
    }

    template <typename REAL>
    REAL image1d<REAL>::translate () const
    {
	return this->_img->_t;
    }


  /***** 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);
    }

    template <typename REAL>
    uint32_t image2d<REAL>::size (uint32_t dim) const
    {
	return this->_img->_size;
    }

    template <typename REAL>
    const REAL *image2d<REAL>::base_addr () const
    {
	return this->_img->_data;
    }

    template <typename REAL>
    mat2x2<REAL> const &image2d<REAL>::world2image () const
    {
	return this->_img->_w2i;
    }

    template <typename REAL>
    real2<REAL> const &image2d<REAL>::translate () const
    {
	return this->_img->_tVec;
    }


  /***** 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);
    }

    template <typename REAL>
    uint32_t image3d<REAL>::size (uint32_t dim) const
    {
	return this->_img->_size;
    }

    template <typename REAL>
    const REAL *image3d<REAL>::base_addr () const
    {
	return this->_img->_data;
    }

    template <typename REAL>
    mat3x3<REAL> const &image3d<REAL>::world2image () const
    {
	return this->_img->_w2i;
    }

    template <typename REAL>
    real3<REAL> const &image3d<REAL>::translate () const
    {
	return this->_img->_tVec;
    }

} // namespace diderot

#include "image-inst.hxx"

#endif //! _DIDEROT_IMAGE_HXX_

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