1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
#ifndef MASK2D_H
#define MASK2D_H
#include <cstring>
#include <memory>
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include "image2d.h"
typedef boost::intrusive_ptr<class Mask2D> Mask2DPtr;
typedef boost::intrusive_ptr<const class Mask2D> Mask2DCPtr;
void swap(Mask2D&, Mask2D&);
void swap(Mask2D&, Mask2D&&);
void swap(Mask2D&&, Mask2D&);
class Mask2D : public boost::intrusive_ref_counter<Mask2D> {
public:
Mask2D();
Mask2D(const Mask2D& source);
Mask2D(Mask2D&& source) noexcept;
~Mask2D() noexcept;
Mask2D& operator=(const Mask2D& rhs);
Mask2D& operator=(Mask2D&& rhs) noexcept;
bool operator==(const Mask2D& rhs) const
{
if(_width != rhs._width || _height != rhs._height)
return false;
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
if(_values[y][x] != rhs._values[y][x])
return false;
}
return true;
}
bool operator!=(const Mask2D& rhs) const { return !(*this == rhs); }
template<typename... Args>
static Mask2DPtr MakePtr(Args&&... args)
{
// This function is to have a generic 'make_<ptr>' function, that e.g. calls
// the more efficient make_shared() when Mask2DPtr is a shared_ptr, but
// also works when Mask2DPtr is a boost::intrusive_ptr.
return Mask2DPtr(new Mask2D(std::forward<Args>(args)...));
}
static Mask2D MakeUnsetMask(size_t width, size_t height)
{
return Mask2D(width, height);
}
template<bool InitValue>
static Mask2D MakeSetMask(size_t width, size_t height)
{
Mask2D newMask(width, height);
memset(newMask._valuesConsecutive, InitValue, newMask._stride * height * sizeof(bool));
return newMask;
}
static Mask2D *CreateUnsetMask(size_t width, size_t height)
{
return new Mask2D(width, height);
}
static Mask2DPtr CreateUnsetMaskPtr(size_t width, size_t height)
{
return Mask2DPtr(new Mask2D(width, height));
}
static Mask2D *CreateUnsetMask(const class Image2D &templateImage);
static Mask2DPtr CreateUnsetMask(Image2DCPtr templateImage)
{
return Mask2DPtr(CreateUnsetMask(*templateImage));
}
template<bool InitValue>
static Mask2D *CreateSetMask(const class Image2D &templateImage);
template<bool InitValue>
static Mask2DPtr CreateSetMask(Image2DCPtr templateImage)
{
return Mask2DPtr(CreateSetMask<InitValue>(*templateImage));
}
template<bool InitValue>
static Mask2D *CreateSetMask(size_t width, size_t height)
{
Mask2D *newMask = new Mask2D(width, height);
memset(newMask->_valuesConsecutive, InitValue, newMask->_stride * height * sizeof(bool));
return newMask;
}
template<bool InitValue>
static Mask2DPtr CreateSetMaskPtr(size_t width, size_t height)
{
return Mask2DPtr(CreateSetMask<InitValue>(width, height));
}
bool Value(size_t x, size_t y) const
{
return _values[y][x];
}
void SetValue(size_t x, size_t y, bool newValue)
{
_values[y][x] = newValue;
}
void SetHorizontalValues(size_t x, size_t y, bool newValue, size_t count)
{
memset(&_values[y][x], newValue, count * sizeof(bool));
}
void SetVerticalValues(size_t x, size_t y, bool newValue, size_t count)
{
for(size_t i=0; i!=count; ++i)
_values[y + i][x] = newValue;;
}
size_t Width() const { return _width; }
size_t Height() const { return _height; }
bool AllFalse() const
{
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
{
if(_values[y][x])
return false;
}
}
return true;
}
/**
* Returns a pointer to one row of data. This can be used to step
* quickly over the data in x direction. Note that the next row
* is not exactly at "one times width", because the number of
* samples in a row is made divisable by four. This makes it
* possible to execute SSE instruction easily.
*
* If you want to skip over a whole row, use the Stride() method
* to determine the intrinsicly used width of one row.
*
* @see Stride()
*/
bool *ValuePtr(size_t x, size_t y)
{
return &_values[y][x];
}
/**
* Returns a constant pointer to one row of data. This can be used to
* step quickly over the data in x direction. Note that the next row
* is not exactly at "one times width", because the number of
* samples in a row is made divisable by four. This makes it
* possible to execute SSE instruction easily.
*
* If you want to skip over a whole row, use the Stride() method
* to determine the intrinsicly used width of one row.
*
* @see Stride()
*/
const bool *ValuePtr(size_t x, size_t y) const
{
return &_values[y][x];
}
bool *Data()
{
return _valuesConsecutive;
}
const bool *Data() const
{
return _valuesConsecutive;
}
/**
* This value specifies the intrinsic width of one row. It is
* normally the first number that is >= Width() and divisable by
* four. When using the ValuePtr(unsigned, unsigned) method,
* this value can be used to step over one row.
*
* @see ValuePtr(unsigned, unsigned)
*/
size_t Stride() const
{
return _stride;
}
template<bool NewValue>
void SetAll()
{
memset(_valuesConsecutive, NewValue, _stride * _height * sizeof(bool));
}
template<bool NewValue>
void SetAllVertically(size_t x)
{
for(size_t y=0;y<_height;++y)
_values[y][x] = NewValue;
}
template<bool NewValue>
void SetAllVertically(size_t startX, size_t endX)
{
for(size_t x=startX;x<endX;++x)
{
for(size_t y=0;y<_height;++y)
_values[y][x] = NewValue;
}
}
template<bool NewValue>
void SetAllHorizontally(size_t y)
{
memset(_values[y], NewValue, _width * sizeof(bool));
}
template<bool BoolValue>
void SetAllHorizontally(size_t startY, size_t endY)
{
memset(_values[startY], BoolValue, _width * sizeof(bool) * (endY - startY));
}
void Invert()
{
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
_values[y][x] = !_values[y][x];
}
}
/**
* Flips the image round the diagonal, i.e., x becomes y and y becomes x.
*/
Mask2D MakeXYFlipped() const
{
Mask2D mask(_height, _width);
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
mask._values[x][y] = _values[y][x];
}
return mask;
}
template<bool BoolValue>
size_t GetCount() const
{
size_t count = 0;
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
if(BoolValue == _values[y][x])
++count;
}
return count;
}
Mask2D ShrinkHorizontally(int factor) const;
Mask2D ShrinkHorizontallyForAveraging(int factor) const;
Mask2D ShrinkVertically(int factor) const;
Mask2D ShrinkVerticallyForAveraging(int factor) const;
void EnlargeHorizontallyAndSet(const Mask2D& smallMask, int factor);
void EnlargeVerticallyAndSet(const Mask2D& smallMask, int factor);
void Join(const Mask2D& other)
{
for(size_t y=0;y<_height;++y) {
for(size_t x=0;x<_width;++x)
SetValue(x, y, other.Value(x, y) || Value(x, y));
}
}
void Intersect(const Mask2D& other)
{
for(size_t y=0;y<_height;++y) {
for(size_t x=0;x<_width;++x)
SetValue(x, y, other.Value(x, y) && Value(x, y));
}
}
Mask2D Trim(size_t startX, size_t startY, size_t endX, size_t endY) const
{
size_t
width = endX - startX,
height = endY - startY;
Mask2D mask(width, height);
for(size_t y=startY;y<endY;++y)
{
for(size_t x=startX;x<endX;++x)
mask.SetValue(x-startX, y-startY, Value(x, y));
}
return mask;
}
void CopyFrom(const Mask2D& source, size_t destX, size_t destY)
{
size_t
x2 = source._width + destX,
y2 = source._height + destY;
if(x2 > _width) x2 = _width;
if(y2 > _height) y2 = _height;
for(size_t y=destY;y<y2;++y)
{
for(size_t x=destX;x<x2;++x)
SetValue(x, y, source.Value(x-destX, y-destY));
}
}
void SwapXY()
{
Mask2D tempMask(_height, _width);
for(size_t y=0;y<_height;++y)
{
for(size_t x=0;x<_width;++x)
{
tempMask.SetValue(y, x, Value(x, y));
}
}
*this = std::move(tempMask);
}
private:
friend void swap(Mask2D&, Mask2D&);
friend void swap(Mask2D&, Mask2D&&);
friend void swap(Mask2D&&, Mask2D&);
Mask2D(size_t width, size_t height);
void allocate();
size_t _width, _height;
size_t _stride;
bool **_values;
bool *_valuesConsecutive;
};
inline void swap(Mask2D& left, Mask2D& right)
{
std::swap(left._width, right._width);
std::swap(left._stride, right._stride);
std::swap(left._height, right._height);
std::swap(left._values, right._values);
std::swap(left._valuesConsecutive, right._valuesConsecutive);
}
inline void swap(Mask2D& left, Mask2D&& right)
{
std::swap(left._width, right._width);
std::swap(left._stride, right._stride);
std::swap(left._height, right._height);
std::swap(left._values, right._values);
std::swap(left._valuesConsecutive, right._valuesConsecutive);
}
inline void swap(Mask2D&& left, Mask2D& right)
{
std::swap(left._width, right._width);
std::swap(left._stride, right._stride);
std::swap(left._height, right._height);
std::swap(left._values, right._values);
std::swap(left._valuesConsecutive, right._valuesConsecutive);
}
#endif
|