ACF $AcfVersion:0$
CScanlineMask.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
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
22namespace iimg
23{
24
25
33 public i2d::CObject2dBase,
34 virtual public IRasterImage
35{
36public:
38 typedef QList<istd::CIntRanges> RangesContainer;
39 typedef std::vector<int> Scanlines; // Index of container element for each scan line, negative value for empty element
40
42
46 bool IsBitmapRegionEmpty() const;
47
54
58 void ResetScanlines(const istd::CIntRange& verticalRange);
59
63 const istd::CIntRanges* GetPixelRanges(int lineIndex) const;
64
69 void CreateFilled(const i2d::CRect& clipArea);
75 bool CreateFromGeometry(const i2d::IObject2d& geometry, const i2d::CRect* clipAreaPtr = NULL);
76
82 void CreateFromCircle(const i2d::CCircle& circle, const i2d::CRect* clipAreaPtr = NULL);
83
89 void CreateFromRectangle(const i2d::CRectangle& rect, const i2d::CRect* clipAreaPtr = NULL);
90
96 void CreateFromAnnulus(const i2d::CAnnulus& annulus, const i2d::CRect* clipAreaPtr = NULL);
97
103 void CreateFromPolygon(const i2d::CPolygon& polygon, const i2d::CRect* clipAreaPtr = NULL);
104
110 void CreateFromTube(const i2d::CTubePolyline& tube, const i2d::CRect* clipAreaPtr = NULL);
111
117 void CreateFromBitmap(const iimg::IBitmap& bitmap, const i2d::CRect* clipAreaPtr = NULL);
118
124 void GetInverted(const i2d::CRect& clipArea, CScanlineMask& result) const;
125
130 void Invert(const i2d::CRect& clipArea);
131
136
141 void GetUnion(const CScanlineMask& mask, CScanlineMask& result) const;
142
147 void Union(const CScanlineMask& mask);
148
153
158 void GetIntersection(const CScanlineMask& mask, CScanlineMask& result) const;
159
164 void Intersection(const CScanlineMask& mask);
165
169 CScanlineMask GetTranslated(int dx, int dy) const;
170
175 void GetTranslated(int dx, int dy, CScanlineMask& result) const;
176
180 void Translate(int dx, int dy);
181
186 void Erode(int leftValue, int rightValue, int topValue, int bottomValue);
187
192 void Dilate(int leftValue, int rightValue, int topValue, int bottomValue);
193
194 // reimplemented (i2d::IObject2d)
195 virtual i2d::CVector2d GetCenter() const override;
196 virtual void MoveCenterTo(const i2d::CVector2d& position) override;
197 virtual i2d::CRectangle GetBoundingBox() const override;
198
199 // reimplemented (iimg::IRasterImage)
200 virtual bool IsEmpty() const override;
201 virtual void ResetImage() override;
202 virtual void ClearImage() override;
203 virtual istd::CIndex2d GetImageSize() const override;
204 virtual int GetComponentsCount() const override;
205 virtual icmm::CVarColor GetColorAt(const istd::CIndex2d& position) const override;
206 virtual bool SetColorAt(const istd::CIndex2d& position, const icmm::CVarColor& color) override;
207 virtual const icmm::IColorModel* GetColorModel() const override;
208
209 // reimplemented (iser::ISerializable)
210 virtual bool Serialize(iser::IArchive& archive) override;
211
212 // reimplemented (istd::IChangeable)
213 virtual int GetSupportedOperations() const override;
214 virtual bool CopyFrom(const istd::IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS) override;
215 virtual bool ResetData(CompatibilityMode mode = CM_WITHOUT_REFS) override;
216
217 bool operator==(const CScanlineMask& mask) const;
218 bool operator!=(const CScanlineMask& mask) const;
219
220 friend uint qHash(const CScanlineMask& key, uint seed);
221
222protected:
223 void EnsureBoundingBoxValid() const;
224 void CalcBoundingBox() const;
225 void InitFromBoundingBox(const i2d::CRectangle& objectBoundingBox, const i2d::CRect* clipAreaPtr);
226
227 template <typename PixelType>
228 void CalculateMaskFromBitmap(const iimg::IBitmap& bitmap, const i2d::CRect* clipAreaPtr = NULL);
229
230private:
231 RangesContainer m_rangesContainer;
232
233 Scanlines m_scanlines;
234
235 int m_firstLinePos;
236
237 mutable i2d::CRect m_boundingBox;
238 mutable bool m_isBoundingBoxValid;
239};
240
241
242// public inline methods
243
244inline bool CScanlineMask::operator!=(const CScanlineMask& mask) const
245{
246 return !operator==(mask);
247}
248
249
250// protected inline methods
251
253{
254 if (!m_isBoundingBoxValid){
256 }
257}
258
259
260// protected methods
261
262template <typename PixelType>
264{
265 InitFromBoundingBox(bitmap.GetBoundingBox(), clipAreaPtr);
266
267 int linesCount = int(m_scanlines.size());
268 if (linesCount <= 0){
269 ResetImage();
270
271 return;
272 }
273
274#if QT_VERSION >= 0x040700
275 m_rangesContainer.reserve(linesCount);
276#endif
277
278 Q_ASSERT(bitmap.GetImageSize().GetY() == (linesCount));
279
280 int imageWidth = bitmap.GetImageSize().GetX();
281
282 for (int lineIndex = 0; lineIndex < linesCount; lineIndex++){
283 istd::CIntRanges rangeList;
284
285 PixelType* imageLinePtr = (PixelType*)bitmap.GetLinePtr(lineIndex);
286 Q_ASSERT(imageLinePtr != NULL);
287
288 int left = -1;
289 int right = -1;
290
291 for (int x = 0; x < imageWidth; ++x){
292 PixelType pixel = *(imageLinePtr + x);
293 if ((pixel > 0) && (left < 0)){
294 left = x;
295 }
296
297 if ((pixel == 0) && (left >= 0)){
298 right = x;
299 if (clipAreaPtr != NULL){
300 if (left < clipAreaPtr->GetLeft()){
301 left = clipAreaPtr->GetLeft();
302 }
303
304 if (right > clipAreaPtr->GetRight()){
305 right = clipAreaPtr->GetRight();
306 }
307 }
308
309 if (left < right){
310 rangeList.InsertSwitchPoint(left);
311 rangeList.InsertSwitchPoint(right);
312 }
313
314 left = -1;
315 right = -1;
316 }
317 }
318
319 if ((left >= 0) && (right < 0)){
320 rangeList.InsertSwitchPoint(left);
321 }
322
323 if (!rangeList.IsEmpty()){
324 m_rangesContainer.push_back(rangeList);
325
326 m_scanlines[lineIndex] = m_rangesContainer.size() - 1;
327 }
328 else{
329 m_scanlines[lineIndex] = -1;
330 }
331 }
332}
333
334
335// related global functions
336
337uint qHash(const CScanlineMask& key, uint seed = 0);
338
339
340} // namespace iimg
341
342
343
344
Definition of graphical annulus object.
Definition CAnnulus.h:20
Definition of graphical circle object.
Definition CCircle.h:22
Base class for 2D-objects implementing interface i2d::IObject2d.
Definition of the data model for a polygon.
Definition CPolygon.h:20
Simple rectangle with integer bounds.
Definition CRect.h:22
int GetRight() const
Definition CRect.h:342
int GetLeft() const
Definition CRect.h:319
Definition of rectangle area orthogonal to axis of coordination system.
Definition CRectangle.h:27
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:26
virtual CRectangle GetBoundingBox() const =0
Get bounding box of this shape.
Generic color implementation with variable number of color components.
Definition CVarColor.h:176
Common interface for color model definitions in the ACF color management system.
Representation of a 2D-region as container of bitmap line scans.
virtual const icmm::IColorModel * GetColorModel() const override
Get the color model related to the image.
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
virtual icmm::CVarColor GetColorAt(const istd::CIndex2d &position) const override
Get color at specified pixel.
void CreateFromAnnulus(const i2d::CAnnulus &annulus, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from annulus.
virtual bool IsEmpty() const override
Return true if this image is empty.
void GetTranslated(int dx, int dy, CScanlineMask &result) const
Calculate translated (moved) mask.
void GetUnion(const CScanlineMask &mask, CScanlineMask &result) const
Get union of two masks.
std::vector< int > Scanlines
virtual i2d::CVector2d GetCenter() const override
Returns center of this 2D-object.
CScanlineMask GetUnion(const CScanlineMask &mask) const
Get union of two masks.
void ResetScanlines(const istd::CIntRange &verticalRange)
Set this mask to empty set for some vertical range.
i2d::CObject2dBase BaseClass
void CalcBoundingBox() const
i2d::CRect GetBoundingRect() const
Get rectangle containing all active pixels.
virtual void ResetImage() override
Reset this image.
virtual bool CopyFrom(const istd::IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS) override
Copy this object from another one.
void Dilate(int leftValue, int rightValue, int topValue, int bottomValue)
Calculate dilatation of this range list with rectangle structured element.
virtual bool ResetData(CompatibilityMode mode=CM_WITHOUT_REFS) override
Reset data to its default state.
void GetIntersection(const CScanlineMask &mask, CScanlineMask &result) const
Get intersection of two masks.
void CreateFromBitmap(const iimg::IBitmap &bitmap, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from a bitmap.
virtual istd::CIndex2d GetImageSize() const override
Get size of this raster image.
QList< istd::CIntRanges > RangesContainer
virtual int GetComponentsCount() const override
Get number of color components.
bool CreateFromGeometry(const i2d::IObject2d &geometry, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from some geometrical object.
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.
void CreateFromPolygon(const i2d::CPolygon &polygon, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from polygon.
bool IsBitmapRegionEmpty() const
Check if region is empty.
const istd::CIntRanges * GetPixelRanges(int lineIndex) const
Get the list of pixel ranges per given line.
friend uint qHash(const CScanlineMask &key, uint seed)
void Union(const CScanlineMask &mask)
Calculate union of this mask and the other one.
void CreateFromCircle(const i2d::CCircle &circle, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from circle.
CScanlineMask GetTranslated(int dx, int dy) const
Calculate translated (moved) mask.
void CalculateMaskFromBitmap(const iimg::IBitmap &bitmap, const i2d::CRect *clipAreaPtr=NULL)
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.
virtual int GetSupportedOperations() const override
Get set of flags for supported operations.
bool operator!=(const CScanlineMask &mask) const
virtual void MoveCenterTo(const i2d::CVector2d &position) override
Move object to position position.
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 CreateFromRectangle(const i2d::CRectangle &rect, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from rectangle.
void Translate(int dx, int dy)
Translated (move) this mask.
virtual i2d::CRectangle GetBoundingBox() const override
Get bounding box of this shape.
void Invert(const i2d::CRect &clipArea)
Invert mask on place.
virtual void ClearImage() override
Cleat this image.
virtual bool SetColorAt(const istd::CIndex2d &position, const icmm::CVarColor &color) override
Set color at specified pixel.
void CreateFromTube(const i2d::CTubePolyline &tube, const i2d::CRect *clipAreaPtr=NULL)
Create 2D-region from tube.
Definition of single plane bitmap.
Definition IBitmap.h:21
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.
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
Index implementation for addressing elements in 2D-space.
Definition CIndex2d.h:21
int GetX() const
Definition CIndex2d.h:96
int GetY() const
Definition CIndex2d.h:108
Common interface for data model objects, which can be changed.
Definition IChangeable.h:28
CompatibilityMode
Control how relationship betweeen objects are interpreted.
@ CM_WITHOUT_REFS
External references are simple ignored.
#define NULL
Definition istd.h:74
Contains the system indenendent definitions of image and related themes.
Definition CBitmap.h:17
uint qHash(const CScanlineMask &key, uint seed=0)
TRanges< int > CIntRanges
Definition TRanges.h:947