CScanlineMask.h
Go to the documentation of this file.
1 #ifndef iimg_CScanlineMask_included
2 #define iimg_CScanlineMask_included
3 
4 
5 // STL includes
6 #include <vector>
7 
8 // Qt includes
9 #include <QtCore/QList>
10 
11 // ACF includes
12 #include <istd/TRanges.h>
13 #include <i2d/CObject2dBase.h>
14 #include <i2d/CRect.h>
15 #include <i2d/CCircle.h>
16 #include <i2d/CAnnulus.h>
17 #include <i2d/CPolygon.h>
18 #include <i2d/CTubePolyline.h>
19 #include <iimg/IBitmap.h>
20 
21 
22 namespace iimg
23 {
24 
25 
30  public i2d::CObject2dBase,
31  virtual public IRasterImage
32 {
33 public:
35  typedef QList<istd::CIntRanges> RangesContainer;
36  typedef std::vector<int> Scanlines; // Index of container element for each scan line, negative value for empty element
37 
38  CScanlineMask();
39 
43  bool IsBitmapRegionEmpty() const;
44 
51 
55  void ResetScanlines(const istd::CIntRange& verticalRange);
56 
60  const istd::CIntRanges* GetPixelRanges(int lineIndex) const;
61 
66  void CreateFilled(const i2d::CRect& clipArea);
72  bool CreateFromGeometry(const i2d::IObject2d& geometry, const i2d::CRect* clipAreaPtr = NULL);
73 
79  void CreateFromCircle(const i2d::CCircle& circle, const i2d::CRect* clipAreaPtr = NULL);
80 
86  void CreateFromRectangle(const i2d::CRectangle& rect, const i2d::CRect* clipAreaPtr = NULL);
87 
93  void CreateFromAnnulus(const i2d::CAnnulus& annulus, const i2d::CRect* clipAreaPtr = NULL);
94 
100  void CreateFromPolygon(const i2d::CPolygon& polygon, const i2d::CRect* clipAreaPtr = NULL);
101 
107  void CreateFromTube(const i2d::CTubePolyline& tube, const i2d::CRect* clipAreaPtr = NULL);
108 
114  void CreateFromBitmap(const iimg::IBitmap& bitmap, const i2d::CRect* clipAreaPtr = NULL);
115 
121  void GetInverted(const i2d::CRect& clipArea, CScanlineMask& result) const;
122 
127  void Invert(const i2d::CRect& clipArea);
128 
132  CScanlineMask GetUnion(const CScanlineMask& mask) const;
133 
138  void GetUnion(const CScanlineMask& mask, CScanlineMask& result) const;
139 
144  void Union(const CScanlineMask& mask);
145 
149  CScanlineMask GetIntersection(const CScanlineMask& mask) const;
150 
155  void GetIntersection(const CScanlineMask& mask, CScanlineMask& result) const;
156 
161  void Intersection(const CScanlineMask& mask);
162 
166  CScanlineMask GetTranslated(int dx, int dy) const;
167 
172  void GetTranslated(int dx, int dy, CScanlineMask& result) const;
173 
177  void Translate(int dx, int dy);
178 
183  void Erode(int leftValue, int rightValue, int topValue, int bottomValue);
184 
189  void Dilate(int leftValue, int rightValue, int topValue, int bottomValue);
190 
191  // reimplemented (i2d::IObject2d)
192  virtual i2d::CVector2d GetCenter() const;
193  virtual void MoveCenterTo(const i2d::CVector2d& position);
194  virtual i2d::CRectangle GetBoundingBox() const;
195 
196  // reimplemented (iimg::IRasterImage)
197  virtual bool IsEmpty() const;
198  virtual void ResetImage();
199  virtual void ClearImage();
200  virtual istd::CIndex2d GetImageSize() const;
201  virtual int GetComponentsCount() const;
202  virtual icmm::CVarColor GetColorAt(const istd::CIndex2d& position) const;
203  virtual bool SetColorAt(const istd::CIndex2d& position, const icmm::CVarColor& color);
204 
205  // reimplemented (iser::ISerializable)
206  virtual bool Serialize(iser::IArchive& archive);
207 
208  // reimplemented (istd::IChangeable)
209  virtual int GetSupportedOperations() const;
210  virtual bool CopyFrom(const istd::IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS);
211  virtual bool ResetData(CompatibilityMode mode = CM_WITHOUT_REFS);
212 
213  bool operator==(const CScanlineMask& mask) const;
214  bool operator!=(const CScanlineMask& mask) const;
215 
216  friend uint qHash(const CScanlineMask& key, uint seed);
217 
218 protected:
219  void EnsureBoundingBoxValid() const;
220  void CalcBoundingBox() const;
221  void InitFromBoundingBox(const i2d::CRectangle& objectBoundingBox, const i2d::CRect* clipAreaPtr);
222 
223  template <typename PixelType>
224  void CalculateMaskFromBitmap(const iimg::IBitmap& bitmap, const i2d::CRect* clipAreaPtr = NULL);
225 
226 private:
227  RangesContainer m_rangesContainer;
228 
229  Scanlines m_scanlines;
230 
231  int m_firstLinePos;
232 
233  mutable i2d::CRect m_boundingBox;
234  mutable bool m_isBoundingBoxValid;
235 };
236 
237 
238 // public inline methods
239 
240 inline bool CScanlineMask::operator!=(const CScanlineMask& mask) const
241 {
242  return !operator==(mask);
243 }
244 
245 
246 // protected inline methods
247 
249 {
250  if (!m_isBoundingBoxValid){
251  CalcBoundingBox();
252  }
253 }
254 
255 
256 // protected methods
257 
258 template <typename PixelType>
259 void CScanlineMask::CalculateMaskFromBitmap(const iimg::IBitmap& bitmap, const i2d::CRect* clipAreaPtr)
260 {
261  InitFromBoundingBox(bitmap.GetBoundingBox(), clipAreaPtr);
262 
263  int linesCount = int(m_scanlines.size());
264  if (linesCount <= 0){
265  ResetImage();
266 
267  return;
268  }
269 
270 #if QT_VERSION >= 0x040700
271  m_rangesContainer.reserve(linesCount);
272 #endif
273 
274  Q_ASSERT(bitmap.GetImageSize().GetY() == (linesCount));
275 
276  int imageWidth = bitmap.GetImageSize().GetX();
277 
278  for (int lineIndex = 0; lineIndex < linesCount; lineIndex++){
279  istd::CIntRanges rangeList;
280 
281  PixelType* imageLinePtr = (PixelType*)bitmap.GetLinePtr(lineIndex);
282  Q_ASSERT(imageLinePtr != NULL);
283 
284  int left = -1;
285  int right = -1;
286 
287  for (int x = 0; x < imageWidth; ++x){
288  PixelType pixel = *(imageLinePtr + x);
289  if ((pixel > 0) && (left < 0)){
290  left = x;
291  }
292 
293  if ((pixel == 0) && (left >= 0)){
294  if (clipAreaPtr != NULL){
295  if (left < clipAreaPtr->GetLeft()){
296  left = clipAreaPtr->GetLeft();
297  }
298 
299  if (right > clipAreaPtr->GetRight()){
300  right = clipAreaPtr->GetRight();
301  }
302  }
303 
304  if (left < right){
305  rangeList.InsertSwitchPoint(left);
306  rangeList.InsertSwitchPoint(right);
307  }
308 
309  left = -1;
310  right = -1;
311  }
312  }
313 
314  if ((left >= 0) && (right < 0)){
315  rangeList.InsertSwitchPoint(left);
316  }
317 
318  if (!rangeList.IsEmpty()){
319  m_rangesContainer.push_back(rangeList);
320 
321  m_scanlines[lineIndex] = m_rangesContainer.size() - 1;
322  }
323  else{
324  m_scanlines[lineIndex] = -1;
325  }
326  }
327 }
328 
329 
330 // related global functions
331 
332 uint qHash(const CScanlineMask& key, uint seed = 0);
333 
334 
335 } // namespace iimg
336 
337 
338 #endif // !iimg_CScanlineMask_included
339 
340 
Definition of the data model for a polygon.
Definition: CPolygon.h:16
void Translate(int dx, int dy)
Translated (move) this mask.
CScanlineMask GetUnion(const CScanlineMask &mask) const
Get union of two masks.
void Intersection(const CScanlineMask &mask)
Calculate intersection of this mask and the other one.
virtual bool SetColorAt(const istd::CIndex2d &position, const icmm::CVarColor &color)
Set color at specified pixel.
bool operator!=(const CScanlineMask &mask) const
virtual istd::CIndex2d GetImageSize() const =0
Get size of this raster image.
virtual icmm::CVarColor GetColorAt(const istd::CIndex2d &position) const
Get color at specified pixel.
External references are simple ignored.
Definition: IChangeable.h:197
CompatibilityMode
Control how relationship betweeen objects are interpreted.
Definition: IChangeable.h:186
void CreateFromBitmap(const iimg::IBitmap &bitmap, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from a bitmap.
Definition of tube region based on polyline.
Definition: CTubePolyline.h:17
virtual int GetComponentsCount() const
Get number of color components.
void CreateFromPolygon(const i2d::CPolygon &polygon, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from polygon.
void CalculateMaskFromBitmap(const iimg::IBitmap &bitmap, const i2d::CRect *clipAreaPtr=NULL)
bool IsEmpty() const
Check if this set is empty.
Definition: TRanges.h:223
int GetRight() const
Definition: CRect.h:342
Simple rectangle with integer bounds.
Definition: CRect.h:21
Index implementation for addressing elements in 2D-space.
Definition: CIndex2d.h:20
Set of ranges.
Definition: TRanges.h:25
void CreateFilled(const i2d::CRect &clipArea)
Create filled 2D-region clipped to rectangle area.
virtual i2d::CRectangle GetBoundingBox() const
Get bounding box of this shape.
const istd::CIntRanges * GetPixelRanges(int lineIndex) const
Get the list of pixel ranges per given line.
void CreateFromRectangle(const i2d::CRectangle &rect, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from rectangle.
void Erode(int leftValue, int rightValue, int topValue, int bottomValue)
Calculate erosion of this range list with rectangle structured element.
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
void Union(const CScanlineMask &mask)
Calculate union of this mask and the other one.
void EnsureBoundingBoxValid() const
void CreateFromCircle(const i2d::CCircle &circle, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from circle.
i2d::CObject2dBase BaseClass
Definition: CScanlineMask.h:34
Definition of graphical annulus object.
Definition: CAnnulus.h:19
QList< istd::CIntRanges > RangesContainer
Definition: CScanlineMask.h:35
bool CreateFromGeometry(const i2d::IObject2d &geometry, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from some geometrical object.
void InitFromBoundingBox(const i2d::CRectangle &objectBoundingBox, const i2d::CRect *clipAreaPtr)
Generic color implementation with variable number of color components.
Definition: CVarColor.h:21
void Invert(const i2d::CRect &clipArea)
Invert mask on place.
virtual i2d::CVector2d GetCenter() const
Returns center of this 2D-object.
int GetX() const
Definition: CIndex2d.h:96
Definition of single plane bitmap.
Definition: IBitmap.h:17
uint qHash(const CScanlineMask &key, uint seed=0)
Represent input/output persistence archive.
Definition: IArchive.h:30
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
std::vector< int > Scanlines
Definition: CScanlineMask.h:36
Definition of rectangle area orthogonal to axis of coordination system.
Definition: CRectangle.h:26
void ResetScanlines(const istd::CIntRange &verticalRange)
Set this mask to empty set for some vertical range.
virtual bool ResetData(CompatibilityMode mode=CM_WITHOUT_REFS)
Reset data to its default state.
CScanlineMask GetTranslated(int dx, int dy) const
Calculate translated (moved) mask.
friend uint qHash(const CScanlineMask &key, uint seed)
Definition of position or mathematical vector on 2D plane.
Definition: CVector2d.h:28
General definition of image contains pixels in regular grid.
Definition: IRasterImage.h:19
CScanlineMask GetIntersection(const CScanlineMask &mask) const
Get intersection of two masks.
Base class for 2D-objects implementing interface i2d::IObject2d.
Definition: CObject2dBase.h:18
Definition of graphical circle object.
Definition: CCircle.h:21
virtual CRectangle GetBoundingBox() const =0
Get bounding box of this shape.
#define NULL
Definition: istd.h:64
Representation of a 2D-region as container of bitmap line scans.
Definition: CScanlineMask.h:29
void CreateFromAnnulus(const i2d::CAnnulus &annulus, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from annulus.
i2d::CRect GetBoundingRect() const
Get rectangle containing all active pixels.
virtual void MoveCenterTo(const i2d::CVector2d &position)
Move object to position position.
bool IsBitmapRegionEmpty() const
Check if region is empty.
bool operator==(const CScanlineMask &mask) const
int GetY() const
Definition: CIndex2d.h:108
Common interface for describing the 2D-objects.
Definition: IObject2d.h:22
void Dilate(int leftValue, int rightValue, int topValue, int bottomValue)
Calculate dilatation of this range list with rectangle structured element.
void CreateFromTube(const i2d::CTubePolyline &tube, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from tube.
virtual bool IsEmpty() const
Return true if this image is empty.
void InsertSwitchPoint(ValueType point)
Insert new switch point.
Definition: TRanges.h:244
void GetInverted(const i2d::CRect &clipArea, CScanlineMask &result) const
Get inverted mask.
virtual int GetSupportedOperations() const
Get set of flags for supported operations.
virtual void ClearImage()
Cleat this image.
virtual const void * GetLinePtr(int positionY) const =0
Get pointer to buffer for single line.
virtual bool CopyFrom(const istd::IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
Copy this object from another one.
virtual istd::CIndex2d GetImageSize() const
Get size of this raster image.
virtual void ResetImage()
Reset this image.
int GetLeft() const
Definition: CRect.h:319
void CalcBoundingBox() const

© 2007-2017 Witold Gantzke and Kirill Lepskiy