Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_IMAGE_ITERATOR_H
Toshihiro Shimizu 890ddd
#define TCG_IMAGE_ITERATOR_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "tcg_ptr.h"
Toshihiro Shimizu 890ddd
#include "tcg_image_ops.h"
Toshihiro Shimizu 890ddd
#include "tcg_point.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <iterator></iterator>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace tcg
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
//    Image Iterator  class
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  The image_iterator class models an iterator accessing pixels of an image along its rows.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename it=""></typename>
Toshihiro Shimizu 890ddd
class image_iterator : public iterator_traits<it>::inheritable_iterator_type</it>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	typedef typename iterator_traits<it>::inheritable_iterator_type iter;</it>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	typedef typename iter::iterator_category iterator_category;
Toshihiro Shimizu 890ddd
	typedef typename iter::value_type value_type;
Toshihiro Shimizu 890ddd
	typedef typename iter::difference_type difference_type;
Toshihiro Shimizu 890ddd
	typedef typename iter::pointer pointer;
Toshihiro Shimizu 890ddd
	typedef typename iter::reference reference;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	image_iterator() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	template <typename img=""></typename>
Toshihiro Shimizu 890ddd
	image_iterator(const Img &img, int x, int y)
Toshihiro Shimizu 890ddd
		: iter(image_traits::pixel(img, x, y)), m_base(image_traits::pixel(img, 0, 0)), m_lx(image_traits::width(img)), m_ly(image_traits::height(img)), m_wrap(image_traits::wrap(img)), m_skew(m_wrap - lx) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x() const { return (iter::operator-(m_base)) % m_wrap; }
Toshihiro Shimizu 890ddd
	int y() const { return (iter::operator-(m_base)) / m_wrap; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_iterator &operator++()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		iter::operator++();
Toshihiro Shimizu 890ddd
		if (x() >= m_lx)
Toshihiro Shimizu 890ddd
			iter::operator+=(m_skew);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	image_iterator operator++(int)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		image_iterator it(*this);
Toshihiro Shimizu 890ddd
		operator++();
Toshihiro Shimizu 890ddd
		return it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_iterator &operator--()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		iter::operator--();
Toshihiro Shimizu 890ddd
		if (x() < 0)
Toshihiro Shimizu 890ddd
			iter::operator-=(m_skew);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	image_iterator operator--(int)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		image_iterator it(*this);
Toshihiro Shimizu 890ddd
		operator--();
Toshihiro Shimizu 890ddd
		return it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_iterator &operator+=(difference_type d)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int yCount = (x() + d) / m_lx;
Toshihiro Shimizu 890ddd
		iter::operator+=((d - yCount * m_lx) + yCount * m_wrap);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	image_iterator operator+(difference_type d) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		image_iterator it(*this);
Toshihiro Shimizu 890ddd
		it += d;
Toshihiro Shimizu 890ddd
		return it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_iterator operator-(difference_type d) const { return operator+(-d); }
Toshihiro Shimizu 890ddd
	image_iterator &operator-=(difference_type d) { return operator+=(-d); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	difference_type operator-(const image_iterator &other) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return (x() - other.x()) + m_lx * (y() - other.y());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	reference operator[](difference_type d) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		const image_iterator &it = operator+(d);
Toshihiro Shimizu 890ddd
		return *it;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	iter m_base;
Toshihiro Shimizu 890ddd
	int m_lx, m_ly, m_wrap, m_skew;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
//    image_edge_iterator class
Toshihiro Shimizu 890ddd
//*********************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
enum _iei_adherence_policy { LEFT_ADHERENCE,
Toshihiro Shimizu 890ddd
							 RIGHT_ADHERENCE };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  The image_edge_iterator class models a forward iterator following the contour of
Toshihiro Shimizu 890ddd
  an image area of uniform color.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="RIGHT_ADHERENCE" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
class image_edge_iterator
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	typedef typename iterator_traits<it>::inheritable_iterator_type iter;</it>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	typedef std::forward_iterator_tag iterator_category;
Toshihiro Shimizu 890ddd
	typedef typename iter::value_type value_type;
Toshihiro Shimizu 890ddd
	typedef typename iter::difference_type difference_type;
Toshihiro Shimizu 890ddd
	typedef typename iter::pointer pointer;
Toshihiro Shimizu 890ddd
	typedef typename iter::reference reference;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	enum { adherence = _adherence };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum Direction { STRAIGHT = 0x0,
Toshihiro Shimizu 890ddd
					 LEFT = 0x1,
Toshihiro Shimizu 890ddd
					 RIGHT = 0x2,
Toshihiro Shimizu 890ddd
					 AMBIGUOUS = 0x4,
Toshihiro Shimizu 890ddd
					 UNKNOWN = 0x8,
Toshihiro Shimizu 890ddd
					 AMBIGUOUS_LEFT = LEFT | AMBIGUOUS,
Toshihiro Shimizu 890ddd
					 AMBIGUOUS_RIGHT = RIGHT | AMBIGUOUS };
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	image_edge_iterator() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	template <typename img=""></typename>
Toshihiro Shimizu 890ddd
	image_edge_iterator(const Img &img, int x, int y, int dirX, int dirY);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const Point &pos() const { return m_pos; }
Toshihiro Shimizu 890ddd
	const Point &dir() const { return m_dir; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const value_type &leftColor() const { return m_leftColor; }
Toshihiro Shimizu 890ddd
	const value_type &rightColor() const { return m_rightColor; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const value_type &color() const { return color(policy<_adherence>()); }
Toshihiro Shimizu 890ddd
	const value_type &oppositeColor() const { return oppositeColor(policy<_adherence>()); }
Toshihiro Shimizu 890ddd
	const value_type &elbowColor() const { return m_elbowColor; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iter leftPixel() const { return m_leftPix; }
Toshihiro Shimizu 890ddd
	iter rightPixel() const { return m_rightPix; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iter pixel() const { return pixel(policy<_adherence>()); }
Toshihiro Shimizu 890ddd
	iter oppositePixel() const { return oppositePixel(policy<_adherence>()); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Direction turn() const { return Direction(m_turn); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	// Iterator functions
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool operator==(const image_edge_iterator &it) const { return (m_pos == it.m_pos) && (m_dir == it.m_dir); }
Toshihiro Shimizu 890ddd
	bool operator!=(const image_edge_iterator &it) const { return !operator==(it); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_edge_iterator &operator++()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		advance(policy<_adherence>());
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	image_edge_iterator operator++(int)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		image_edge_iterator temp(*this);
Toshihiro Shimizu 890ddd
		operator++();
Toshihiro Shimizu 890ddd
		return temp;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	void pixels(iter pixLeft, iter pixRight);
Toshihiro Shimizu 890ddd
	void colors(value_type &leftColor, value_type &rightColor);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void turnLeft()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int temp = m_dir.x;
Toshihiro Shimizu 890ddd
		m_dir.x = -m_dir.y;
Toshihiro Shimizu 890ddd
		m_dir.y = temp;
Toshihiro Shimizu 890ddd
		m_turn = LEFT;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void turnRight()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int temp = m_dir.x;
Toshihiro Shimizu 890ddd
		m_dir.x = m_dir.y;
Toshihiro Shimizu 890ddd
		m_dir.y = -temp;
Toshihiro Shimizu 890ddd
		m_turn = RIGHT;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void turn(const value_type &newLeftColor, const value_type &newRightColor)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		turn(newLeftColor, newRightColor, policy<_adherence>());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	void turnAmbiguous(const value_type &newLeftColor, const value_type &newRightColor);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	template <_iei_adherence_policy>
Toshihiro Shimizu 890ddd
	struct policy {
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const value_type &color(policy<left_adherence>) const { return m_leftColor; }</left_adherence>
Toshihiro Shimizu 890ddd
	const value_type &color(policy<right_adherence>) const { return m_rightColor; }</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	const value_type &oppositeColor(policy<left_adherence>) const { return m_rightColor; }</left_adherence>
Toshihiro Shimizu 890ddd
	const value_type &oppositeColor(policy<right_adherence>) const { return m_leftColor; }</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iter pixel(policy<left_adherence>) const { return m_leftPix; }</left_adherence>
Toshihiro Shimizu 890ddd
	iter pixel(policy<right_adherence>) const { return m_rightPix; }</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	iter oppositePixel(policy<left_adherence>) const { return m_rightPix; }</left_adherence>
Toshihiro Shimizu 890ddd
	iter oppositePixel(policy<right_adherence>) const { return m_leftPix; }</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void turn(const value_type &newLeftColor, const value_type &newRightColor, policy<left_adherence>);</left_adherence>
Toshihiro Shimizu 890ddd
	void turn(const value_type &newLeftColor, const value_type &newRightColor, policy<right_adherence>);</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void advance(policy<left_adherence>);</left_adherence>
Toshihiro Shimizu 890ddd
	void advance(policy<right_adherence>);</right_adherence>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	int m_lx_1, m_ly_1, m_wrap;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Point m_pos, m_dir;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	value_type m_leftColor, m_rightColor, m_outsideColor, m_elbowColor;
Toshihiro Shimizu 890ddd
	iter m_pix, m_leftPix, m_rightPix;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_turn;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace tcg
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // TCG_IMAGE_ITERATOR_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=====================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef INCLUDE_HPP
Toshihiro Shimizu 890ddd
#include "hpp/image_iterator.hpp"
Toshihiro Shimizu 890ddd
#endif // INCLUDE_HPP