Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_IMAGE_ITERATOR_HPP
Toshihiro Shimizu 890ddd
#define TCG_IMAGE_ITERATOR_HPP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "../image_iterator.h"
Toshihiro Shimizu 890ddd
#include "../pixel_ops.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***************************************************************************
Toshihiro Shimizu 890ddd
//    image_edge_iterator  implementation
Toshihiro Shimizu 890ddd
//***************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
template <typename img=""></typename>
Shinya Kitaoka 120a6e
image_edge_iterator<it, _adherence="">::image_edge_iterator(const Img &img, int x,</it,>
Shinya Kitaoka 120a6e
                                                         int y, int dirX,
Shinya Kitaoka 120a6e
                                                         int dirY)
Shinya Kitaoka 120a6e
    : m_lx_1(image_traits::width(img) - 1)
Shinya Kitaoka 120a6e
    , m_ly_1(image_traits::height(img) - 1)
Shinya Kitaoka 120a6e
    , m_wrap(image_traits::wrap(img))
Shinya Kitaoka 120a6e
    , m_pos(x, y)
Shinya Kitaoka 120a6e
    , m_dir(dirX, dirY)
Shinya Kitaoka 120a6e
    , m_outsideColor(image_traits::outsideColor(img))
Shinya Kitaoka 120a6e
    , m_elbowColor(m_outsideColor)
Shinya Kitaoka 120a6e
    , m_pix(image_traits::pixel(img, x, y))
Shinya Kitaoka 120a6e
    , m_turn(UNKNOWN) {
Shinya Kitaoka 120a6e
  pixels(m_leftPix, m_rightPix);
Shinya Kitaoka 120a6e
  colors(m_leftColor, m_rightColor);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Shinya Kitaoka 120a6e
inline void image_edge_iterator<it, _adherence="">::pixels(iter pixLeft,</it,>
Shinya Kitaoka 120a6e
                                                        iter pixRight) {
Shinya Kitaoka 120a6e
  if (m_dir.y)
Shinya Kitaoka 120a6e
    if (m_dir.y > 0)
Shinya Kitaoka 120a6e
      pixLeft = m_pix - 1, pixRight = m_pix;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      pixLeft = m_pix - m_wrap, pixRight = pixLeft - 1;
Shinya Kitaoka 120a6e
  else if (m_dir.x > 0)
Shinya Kitaoka 120a6e
    pixLeft = m_pix, pixRight = m_pix - m_wrap;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    pixRight = m_pix - 1, pixLeft = pixRight - m_wrap;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
inline void image_edge_iterator<it, _adherence="">::colors(</it,>
Shinya Kitaoka 120a6e
    value_type &leftColor, value_type &rightColor) {
Shinya Kitaoka 120a6e
  if (m_dir.y)
Shinya Kitaoka 120a6e
    if (m_dir.y > 0) {
Shinya Kitaoka 120a6e
      if (m_pos.y > m_ly_1)
Shinya Kitaoka 120a6e
        leftColor = rightColor = m_outsideColor;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        leftColor  = (m_pos.x > 0) ? *m_leftPix : m_outsideColor;
Shinya Kitaoka 120a6e
        rightColor = (m_pos.x <= m_lx_1) ? *m_rightPix : m_outsideColor;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (m_pos.y < 1)
Shinya Kitaoka 120a6e
        leftColor = rightColor = m_outsideColor;
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        leftColor  = (m_pos.x <= m_lx_1) ? *m_leftPix : m_outsideColor;
Shinya Kitaoka 120a6e
        rightColor = (m_pos.x > 0) ? *m_rightPix : m_outsideColor;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  else if (m_dir.x > 0) {
Shinya Kitaoka 120a6e
    if (m_pos.x > m_lx_1)
Shinya Kitaoka 120a6e
      leftColor = rightColor = m_outsideColor;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      leftColor  = (m_pos.y <= m_ly_1) ? *m_leftPix : m_outsideColor;
Shinya Kitaoka 120a6e
      rightColor = (m_pos.y > 0) ? *m_rightPix : m_outsideColor;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (m_pos.x < 1)
Shinya Kitaoka 120a6e
      leftColor = rightColor = m_outsideColor;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      leftColor  = (m_pos.y > 0) ? *m_leftPix : m_outsideColor;
Shinya Kitaoka 120a6e
      rightColor = (m_pos.y <= m_ly_1) ? *m_rightPix : m_outsideColor;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
inline void image_edge_iterator<it, _adherence="">::turn(</it,>
Shinya Kitaoka 120a6e
    const value_type &newLeftColor, const value_type &newRightColor,
Shinya Kitaoka 120a6e
    policy<right_adherence>) {</right_adherence>
Shinya Kitaoka 120a6e
  if (newLeftColor == m_rightColor) {
Shinya Kitaoka 120a6e
    if (newRightColor == m_leftColor)
Shinya Kitaoka 120a6e
      turnAmbiguous(newLeftColor, newRightColor);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      turnLeft();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (newRightColor != m_rightColor)
Shinya Kitaoka 120a6e
      turnRight();
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      m_turn = STRAIGHT;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_elbowColor = newLeftColor;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pixels(m_leftPix, m_rightPix);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
inline void image_edge_iterator<it, _adherence="">::turn(</it,>
Shinya Kitaoka 120a6e
    const value_type &newLeftColor, const value_type &newRightColor,
Shinya Kitaoka 120a6e
    policy<left_adherence>) {</left_adherence>
Shinya Kitaoka 120a6e
  if (newRightColor == m_leftColor) {
Shinya Kitaoka 120a6e
    if (newLeftColor == m_rightColor)
Shinya Kitaoka 120a6e
      turnAmbiguous(newLeftColor, newRightColor);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      turnRight();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (newLeftColor != m_leftColor)
Shinya Kitaoka 120a6e
      turnLeft();
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      m_turn = STRAIGHT;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_elbowColor = newRightColor;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pixels(m_leftPix, m_rightPix);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Toshihiro Shimizu 890ddd
inline void image_edge_iterator<it, _adherence="">::turnAmbiguous(</it,>
Shinya Kitaoka 120a6e
    const value_type &newLeftColor, const value_type &newRightColor) {
Shinya Kitaoka 120a6e
  UCHAR count1 = 0, count2 = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  value_type val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Check the 4x4 neighbourhood and connect the minority color
Shinya Kitaoka 120a6e
  if (m_pos.x > 2) {
Shinya Kitaoka 120a6e
    val = *(m_pix - 2);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    val = *(m_pix - 2 - m_wrap);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_pos.x < m_lx_1) {
Shinya Kitaoka 120a6e
    val = *(m_pix + 1);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    val = *(m_pix + 1 - m_wrap);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_pos.y > 2) {
Shinya Kitaoka 120a6e
    int wrap2 = m_wrap << 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    val = *(m_pix - wrap2);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    val = *(m_pix - wrap2 - 1);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_pos.y < m_ly_1) {
Shinya Kitaoka 120a6e
    val = *(m_pix + m_wrap);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    val = *(m_pix + m_wrap - 1);
Shinya Kitaoka 120a6e
    if (val == m_leftColor)
Shinya Kitaoka 120a6e
      ++count1;
Shinya Kitaoka 120a6e
    else if (val == m_rightColor)
Shinya Kitaoka 120a6e
      ++count2;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Minority connection - join the one with less count
Shinya Kitaoka 120a6e
  if (count1 <= count2)
Shinya Kitaoka 120a6e
    turnRight();  // Join m_leftColor == newRightColor
Shinya Kitaoka 120a6e
  else if (count1 > count2)
Shinya Kitaoka 120a6e
    turnLeft();  // Join m_rightColor == newLeftColor
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_turn |= AMBIGUOUS;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Shinya Kitaoka 120a6e
void image_edge_iterator<it, _adherence="">::advance(policy<right_adherence>) {</right_adherence></it,>
Shinya Kitaoka 120a6e
  value_type newLeftColor = m_leftColor, newRightColor = m_rightColor;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int pixAdd = m_dir.y * m_wrap + m_dir.x;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_pos.x += m_dir.x, m_pos.y += m_dir.y;
Shinya Kitaoka 120a6e
  m_pix += pixAdd, m_leftPix += pixAdd, m_rightPix += pixAdd;
Shinya Kitaoka 120a6e
  m_leftColor = newLeftColor;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  colors(newLeftColor, newRightColor);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  turn(newLeftColor, newRightColor);
Shinya Kitaoka 120a6e
  colors(m_leftColor, m_rightColor);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename _adherence="" _iei_adherence_policy="" it,=""></typename>
Shinya Kitaoka 120a6e
void image_edge_iterator<it, _adherence="">::advance(policy<left_adherence>) {</left_adherence></it,>
Shinya Kitaoka 120a6e
  value_type newLeftColor = m_leftColor, newRightColor = m_rightColor;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int pixAdd = m_dir.y * m_wrap + m_dir.x;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_pos.x += m_dir.x, m_pos.y += m_dir.y;
Shinya Kitaoka 120a6e
  m_pix += pixAdd, m_leftPix += pixAdd, m_rightPix += pixAdd;
Shinya Kitaoka 120a6e
  m_rightColor = newRightColor;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  colors(newLeftColor, newRightColor);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  turn(newLeftColor, newRightColor);
Shinya Kitaoka 120a6e
  colors(m_leftColor, m_rightColor);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_IMAGE_ITERATOR_HPP