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

© Witold Gantzke and Kirill Lepskiy