tahoma2d/toonz/sources/include/tcg/image_iterator.h

256 lines
7 KiB
C
Raw Normal View History

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