1 // #pragma once // could not use by INCLUDE_HPP
2 
3 #ifndef RASTER_EDGE_ITERATOR_H
4 #define RASTER_EDGE_ITERATOR_H
5 
6 #include "traster.h"
7 
8 namespace TRop {
9 namespace borders {
10 
11 //*********************************************************************************************************
12 //    RasterEdgeIterator class
13 //*********************************************************************************************************
14 
15 /*!
16   The RasterEdgeIterator class models a forward iterator traversing a border of
17   a
18   raster image.
19 */
20 template <typename PixelSelector>
21 class RasterEdgeIterator {
22 public:
23   typedef PixelSelector selector_type;
24   typedef typename PixelSelector::pixel_type pixel_type;
25   typedef typename PixelSelector::value_type value_type;
26   typedef TRasterT<pixel_type> raster_type;
27   typedef TRasterPT<pixel_type> raster_typeP;
28 
29   enum {
30     STRAIGHT        = 0x0,
31     LEFT            = 0x1,
32     RIGHT           = 0x2,
33     AMBIGUOUS       = 0x4,
34     AMBIGUOUS_LEFT  = LEFT | AMBIGUOUS,
35     AMBIGUOUS_RIGHT = RIGHT | AMBIGUOUS,
36     UNKNOWN         = 0x8
37   };
38 
39 private:
40   raster_typeP m_ras;
41   selector_type m_selector;
42 
43   int m_lx_1, m_ly_1, m_wrap;
44 
45   value_type m_leftColor, m_rightColor, m_elbowColor;
46   pixel_type *m_leftPix, *m_rightPix;
47 
48   bool m_rightSide;
49   int m_turn;
50 
51   TPoint m_pos, m_dir;
52 
53 public:
54   RasterEdgeIterator(const raster_typeP &rin, const selector_type &selector,
55                      const TPoint &pos, const TPoint &dir,
56                      int adherence = RIGHT);
57 
58   void setEdge(const TPoint &pos, const TPoint &dir);
59 
raster()60   const raster_typeP &raster() const { return m_ras; }
selector()61   const selector_type &selector() const { return m_selector; }
62 
pos()63   const TPoint &pos() const { return m_pos; }
dir()64   const TPoint &dir() const { return m_dir; }
65 
leftColor()66   const value_type &leftColor() const { return m_leftColor; }
rightColor()67   const value_type &rightColor() const { return m_rightColor; }
68 
color()69   const value_type &color() const {
70     return m_rightSide ? m_rightColor : m_leftColor;
71   }
otherColor()72   const value_type &otherColor() const {
73     return m_rightSide ? m_leftColor : m_rightColor;
74   }
elbowColor()75   const value_type &elbowColor() const { return m_elbowColor; }
76 
leftPix()77   pixel_type *leftPix() const { return m_leftPix; }
rightPix()78   pixel_type *rightPix() const { return m_rightPix; }
79 
pix()80   pixel_type *pix() const { return m_rightSide ? m_rightPix : m_leftPix; }
otherPix()81   pixel_type *otherPix() const { return m_rightSide ? m_leftPix : m_rightPix; }
82 
turn()83   int turn() const { return m_turn; }
84 
setAdherence(int side)85   void setAdherence(int side) { m_rightSide = (side == RIGHT); }
adherence()86   int adherence() const { return m_rightSide ? RIGHT : LEFT; }
87 
88   RasterEdgeIterator &operator++();
89 
90   bool operator==(const RasterEdgeIterator &it) const {
91     return m_pos == it.m_pos && m_dir == it.m_dir;
92   }
93   bool operator!=(const RasterEdgeIterator &it) const {
94     return !operator==(it);
95   }
96 
97 private:
98   void pixels(pixel_type *&pixLeft, pixel_type *&pixRight);
99   void colors(value_type &leftColor, value_type &rightColor);
100   void turn(const value_type &newLeftColor, const value_type &newRightColor);
turnLeft()101   void turnLeft() {
102     int temp = m_dir.x;
103     m_dir.x  = -m_dir.y;
104     m_dir.y  = temp;
105     m_turn   = LEFT;
106   }
turnRight()107   void turnRight() {
108     int temp = m_dir.x;
109     m_dir.x  = m_dir.y;
110     m_dir.y  = -temp;
111     m_turn   = RIGHT;
112   }
113   void turnAmbiguous(const value_type &newLeftColor,
114                      const value_type &newRightColor);
115 };
116 }
117 }  // namespace TRop::borders
118 
119 #endif  // RASTER_EDGE_ITERATOR_H
120 
121 //=====================================================================================
122 
123 #ifdef INCLUDE_HPP
124 #include "raster_edge_iterator.hpp"
125 #endif  // INCLUDE_HPP
126