/usr/include/vigra/multi_shape.hxx is in libvigraimpex-dev 1.10.0+dfsg-3ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | /************************************************************************/
/* */
/* Copyright 2011-2012 by Stefan Schmidt and Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_MULTI_SHAPE_HXX
#define VIGRA_MULTI_SHAPE_HXX
#include <sys/types.h>
#include "tinyvector.hxx"
#include "array_vector.hxx"
#include "numerictraits.hxx"
namespace vigra {
/** \addtogroup MultiIteratorGroup Multi-dimensional Shapes and Array Iterators
\brief Shape objects and general iterators for arrays of arbitrary dimension.
*/
//@{
/** Index type for a single dimension of a MultiArrayView or
MultiArray.
*/
typedef std::ptrdiff_t MultiArrayIndex;
/********************************************************/
/* */
/* Singleband and Multiband */
/* */
/********************************************************/
template <class T>
struct Singleband // the resulting MultiArray has no explicit channel axis
// (i.e. the number of channels is implicitly one)
{
typedef T value_type;
};
template <class T>
struct Multiband // the last axis is explicitly designated as channel axis
{
typedef T value_type;
};
template<class T>
struct NumericTraits<Singleband<T> >
: public NumericTraits<T>
{};
template<class T>
struct NumericTraits<Multiband<T> >
{
typedef Multiband<T> Type;
/*
typedef int Promote;
typedef unsigned int UnsignedPromote;
typedef double RealPromote;
typedef std::complex<RealPromote> ComplexPromote;
*/
typedef Type ValueType;
typedef typename NumericTraits<T>::isIntegral isIntegral;
typedef VigraFalseType isScalar;
typedef typename NumericTraits<T>::isSigned isSigned;
typedef typename NumericTraits<T>::isSigned isOrdered;
typedef typename NumericTraits<T>::isSigned isComplex;
/*
static signed char zero() { return 0; }
static signed char one() { return 1; }
static signed char nonZero() { return 1; }
static signed char min() { return SCHAR_MIN; }
static signed char max() { return SCHAR_MAX; }
#ifdef NO_INLINE_STATIC_CONST_DEFINITION
enum { minConst = SCHAR_MIN, maxConst = SCHAR_MIN };
#else
static const signed char minConst = SCHAR_MIN;
static const signed char maxConst = SCHAR_MIN;
#endif
static Promote toPromote(signed char v) { return v; }
static RealPromote toRealPromote(signed char v) { return v; }
static signed char fromPromote(Promote v) {
return ((v < SCHAR_MIN) ? SCHAR_MIN : (v > SCHAR_MAX) ? SCHAR_MAX : v);
}
static signed char fromRealPromote(RealPromote v) {
return ((v < 0.0)
? ((v < (RealPromote)SCHAR_MIN)
? SCHAR_MIN
: static_cast<signed char>(v - 0.5))
: (v > (RealPromote)SCHAR_MAX)
? SCHAR_MAX
: static_cast<signed char>(v + 0.5));
}
*/
};
namespace detail {
/********************************************************/
/* */
/* defaultStride */
/* */
/********************************************************/
/* generates the stride for a gapless shape.
*/
template <int N>
inline TinyVector <MultiArrayIndex, N>
defaultStride(const TinyVector <MultiArrayIndex, N> &shape)
{
TinyVector <MultiArrayIndex, N> ret;
ret [0] = 1;
for (int i = 1; i < (int)N; ++i)
ret [i] = ret [i-1] * shape [i-1];
return ret;
}
/* generates the stride for a gapless shape.
*/
template <int N>
inline TinyVector <MultiArrayIndex, N>
defaultMultibandStride(const TinyVector <MultiArrayIndex, N> &shape)
{
TinyVector <MultiArrayIndex, N> ret;
ret [N-1] = 1;
for (int i = 0; i < (int)N-1; ++i)
{
int j = (i + int(N - 1)) % N;
ret [i] = ret [j] * shape [j];
}
return ret;
}
/********************************************************/
/* */
/* ResolveMultiband */
/* */
/********************************************************/
template <class T>
struct ResolveMultiband
{
typedef T type;
typedef StridedArrayTag Stride;
static const bool value = false;
template <int N>
static TinyVector <MultiArrayIndex, N>
defaultStride(const TinyVector <MultiArrayIndex, N> &shape)
{
return vigra::detail::defaultStride(shape);
}
};
template <class T>
struct ResolveMultiband<Singleband<T> >
{
typedef T type;
typedef StridedArrayTag Stride;
static const bool value = false;
template <int N>
static TinyVector <MultiArrayIndex, N>
defaultStride(const TinyVector <MultiArrayIndex, N> &shape)
{
return vigra::detail::defaultStride(shape);
}
};
template <class T>
struct ResolveMultiband<Multiband<T> >
{
typedef T type;
typedef StridedArrayTag Stride;
static const bool value = true;
template <int N>
static TinyVector <MultiArrayIndex, N>
defaultStride(const TinyVector <MultiArrayIndex, N> &shape)
{
return vigra::detail::defaultMultibandStride(shape);
}
};
} // namespace detail
template <unsigned int N, class T, class C = StridedArrayTag>
class MultiArrayView;
template <unsigned int N, class T,
class A = std::allocator<typename vigra::detail::ResolveMultiband<T>::type> >
class MultiArray;
/** Traits class for the difference type of all MultiIterator, MultiArrayView, and
MultiArray variants.
*/
template <unsigned int N>
class MultiArrayShape
{
public:
/** The difference type of all MultiIterator, MultiArrayView, and
MultiArray variants.
*/
typedef TinyVector<MultiArrayIndex, N> type;
};
typedef MultiArrayShape<1>::type Shape1; ///< shape type for MultiArray<1, T>
typedef MultiArrayShape<2>::type Shape2; ///< shape type for MultiArray<2, T>
typedef MultiArrayShape<3>::type Shape3; ///< shape type for MultiArray<3, T>
typedef MultiArrayShape<4>::type Shape4; ///< shape type for MultiArray<4, T>
typedef MultiArrayShape<5>::type Shape5; ///< shape type for MultiArray<5, T>
/** \brief Choose the neighborhood system in a dimension-independent way.
DirectNeighborhood corresponds to 4-neighborhood in 2D and 6-neighborhood in 3D, whereas
IndirectNeighborhood means 8-neighborhood in 2D and 26-neighborhood in 3D. The general
formula for N dimensions are 2*N for direct neighborhood and 3^N-1 for indirect neighborhood.
*/
enum NeighborhoodType {
DirectNeighborhood=0, ///< use only direct neighbors
IndirectNeighborhood=1 ///< use direct and indirect neighbors
};
// Helper functions
namespace detail {
/********************************************************/
/* */
/* CoordinateToScanOrder */
/* */
/********************************************************/
/* Convert multi-dimensional index (i.e. a grid coordinate) to scan-order index.
*/
template <int K>
struct CoordinateToScanOrder
{
template <int N, class D1, class D2, class D3, class D4>
static MultiArrayIndex
exec(const TinyVectorBase <MultiArrayIndex, N, D1, D2> &shape,
const TinyVectorBase <MultiArrayIndex, N, D3, D4> & coordinate)
{
return coordinate[N-K] + shape[N-K] * CoordinateToScanOrder<K-1>::exec(shape, coordinate);
}
};
template <>
struct CoordinateToScanOrder<1>
{
template <int N, class D1, class D2, class D3, class D4>
static MultiArrayIndex
exec(const TinyVectorBase <MultiArrayIndex, N, D1, D2> & /*shape*/,
const TinyVectorBase <MultiArrayIndex, N, D3, D4> & coordinate)
{
return coordinate[N-1];
}
};
/********************************************************/
/* */
/* ScanOrderToCoordinate */
/* */
/********************************************************/
/* Convert scan-order index to multi-dimensional index (i.e. a grid coordinate).
*/
template <int K>
struct ScanOrderToCoordinate
{
template <int N>
static void
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> &shape,
TinyVector <MultiArrayIndex, N> & result)
{
result[N-K] = (d % shape[N-K]);
ScanOrderToCoordinate<K-1>::exec(d / shape[N-K], shape, result);
}
};
template <>
struct ScanOrderToCoordinate<1>
{
template <int N>
static void
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> & /*shape*/,
TinyVector <MultiArrayIndex, N> & result)
{
result[N-1] = d;
}
};
/********************************************************/
/* */
/* ScanOrderToOffset */
/* */
/********************************************************/
/* transforms an index in scan order sense to a pointer offset in a possibly
strided, multi-dimensional array.
*/
template <int K>
struct ScanOrderToOffset
{
template <int N>
static MultiArrayIndex
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> &shape,
const TinyVector <MultiArrayIndex, N> & stride)
{
return stride[N-K] * (d % shape[N-K]) +
ScanOrderToOffset<K-1>::exec(d / shape[N-K], shape, stride);
}
};
template <>
struct ScanOrderToOffset<1>
{
template <int N>
static MultiArrayIndex
exec(MultiArrayIndex d, const TinyVector <MultiArrayIndex, N> & /*shape*/,
const TinyVector <MultiArrayIndex, N> & stride)
{
return stride[N-1] * d;
}
};
/********************************************************/
/* */
/* ScanOrderToOffset */
/* */
/********************************************************/
/* transforms a multi-dimensional index (grid coordinate) to a pointer offset in a possibly
strided, multi-dimensional array.
*/
template <class C>
struct CoordinatesToOffest
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x)
{
return stride[0] * x;
}
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x, MultiArrayIndex y)
{
return stride[0] * x + stride[1] * y;
}
};
template <>
struct CoordinatesToOffest<UnstridedArrayTag>
{
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & /*stride*/, MultiArrayIndex x)
{
return x;
}
template <int N>
static MultiArrayIndex
exec(const TinyVector <MultiArrayIndex, N> & stride, MultiArrayIndex x, MultiArrayIndex y)
{
return x + stride[1] * y;
}
};
/********************************************************/
/* */
/* RelativeToAbsoluteCoordinate */
/* */
/********************************************************/
/* transforms a coordinate object with negative indices into the corresponding
'shape - abs(index)'.
*/
template <int M>
struct RelativeToAbsoluteCoordinate
{
template <int N>
static void
exec(const TinyVector<MultiArrayIndex, N> & shape, TinyVector<MultiArrayIndex, N> & coord)
{
RelativeToAbsoluteCoordinate<M-1>::exec(shape, coord);
if(coord[M] < 0)
coord[M] += shape[M];
}
};
template <>
struct RelativeToAbsoluteCoordinate<0>
{
template <int N>
static void
exec(const TinyVector<MultiArrayIndex, N> & shape, TinyVector<MultiArrayIndex, N> & coord)
{
if(coord[0] < 0)
coord[0] += shape[0];
}
};
/********************************************************/
/* */
/* BorderTypeImpl */
/* */
/********************************************************/
// a border type is a compact bit-wise encoding of the fact that a
// given coordinate is at the border of the ROI. Each border corresponds
// to one bit in the encoding, e.g. the left, right, top, bottom borders
// of a 2D image are represented by bits 0 to 3 respectively.
// If a bit is set, the point in question is at the corresponding border.
// A code of all zeros therefore means that the point is in the interior
// of the ROI
template <unsigned int N, unsigned int DIMENSION=N-1>
struct BorderTypeImpl
{
typedef typename MultiArrayShape<N>::type shape_type;
static unsigned int exec(shape_type const & point, shape_type const & shape)
{
unsigned int res = BorderTypeImpl<N, DIMENSION-1>::exec(point, shape);
if(point[DIMENSION] == 0)
res |= (1 << 2*DIMENSION);
if(point[DIMENSION] == shape[DIMENSION]-1)
res |= (2 << 2*DIMENSION);
return res;
}
};
template <unsigned int N>
struct BorderTypeImpl<N, 0>
{
typedef typename MultiArrayShape<N>::type shape_type;
static const unsigned int DIMENSION = 0;
static unsigned int exec(shape_type const & point, shape_type const & shape)
{
unsigned int res = 0;
if(point[DIMENSION] == 0)
res |= (1 << 2*DIMENSION);
if(point[DIMENSION] == shape[DIMENSION]-1)
res |= (2 << 2*DIMENSION);
return res;
}
};
/********************************************************/
/* */
/* makeArrayNeighborhood */
/* */
/********************************************************/
// Create the offsets to all direct neighbors, starting from the given Level (=dimension)
// and append them to the given array. The algorithm is designed so that the offsets are
// sorted by ascending strides. This has two important consequences:
// * The first half of the array contains the causal neighbors (negative strides),
// the second half the anti-causal ones (positive strides), where 'causal' refers
// to all scan-order predecessors of the center pixel, and 'anticausal' to its successors.
// * For any neighbor k, its opposite (=point-reflected) neighbor is located at index
// 'N-1-k', where N is the total number of neighbors.
// The function 'exists' returns an array of flags that contains 'true' when the corresponding
// neighbor is inside the ROI for the given borderType, 'false' otherwise.
template <unsigned int Level>
struct MakeDirectArrayNeighborhood
{
template <class Array>
static void offsets(Array & a)
{
typedef typename Array::value_type Shape;
Shape point;
point[Level] = -1;
a.push_back(point);
MakeDirectArrayNeighborhood<Level-1>::offsets(a);
point[Level] = 1;
a.push_back(point);
}
template <class Array>
static void exists(Array & a, unsigned int borderType)
{
a.push_back((borderType & (1 << 2*Level)) == 0);
MakeDirectArrayNeighborhood<Level-1>::exists(a, borderType);
a.push_back((borderType & (2 << 2*Level)) == 0);
}
};
template <>
struct MakeDirectArrayNeighborhood<0>
{
template <class Array>
static void offsets(Array & a)
{
typedef typename Array::value_type Shape;
Shape point;
point[0] = -1;
a.push_back(point);
point[0] = 1;
a.push_back(point);
}
template <class Array>
static void exists(Array & a, unsigned int borderType)
{
a.push_back((borderType & 1) == 0);
a.push_back((borderType & 2) == 0);
}
};
// Likewise, create the offsets to all indirect neighbors according to the same rules.
template <unsigned int Level>
struct MakeIndirectArrayNeighborhood
{
template <class Array, class Shape>
static void offsets(Array & a, Shape point, bool isCenter = true)
{
point[Level] = -1;
MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point, false);
point[Level] = 0;
MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point, isCenter);
point[Level] = 1;
MakeIndirectArrayNeighborhood<Level-1>::offsets(a, point, false);
}
template <class Array>
static void exists(Array & a, unsigned int borderType, bool isCenter = true)
{
if((borderType & (1 << 2*Level)) == 0)
MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType, false);
else
MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType, isCenter);
if((borderType & (2 << 2*Level)) == 0)
MakeIndirectArrayNeighborhood<Level-1>::exists(a, borderType, false);
else
MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
}
template <class Array>
static void markOutside(Array & a)
{
// Call markOutside() three times, for each possible offset at (Level-1)
MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
MakeIndirectArrayNeighborhood<Level-1>::markOutside(a);
}
};
template <>
struct MakeIndirectArrayNeighborhood<0>
{
template <class Array, class Shape>
static void offsets(Array & a, Shape point, bool isCenter = true)
{
point[0] = -1;
a.push_back(point);
if(!isCenter) // the center point is not a neighbor, it's just convenient to do the enumeration this way...
{
point[0] = 0;
a.push_back(point);
}
point[0] = 1;
a.push_back(point);
}
template <class Array>
static void exists(Array & a, unsigned int borderType, bool isCenter = true)
{
a.push_back((borderType & 1) == 0);
if(!isCenter)
{
a.push_back(true);
}
a.push_back((borderType & 2) == 0);
}
template <class Array>
static void markOutside(Array & a)
{
// Push 'false' three times, for each possible offset at level 0, whenever the point was
// outside the ROI in one of the higher levels.
a.push_back(false);
a.push_back(false);
a.push_back(false);
}
};
// Create the list of neighbor offsets for the given neighborhood type
// and dimension (the dimension is implicitly defined by the Shape type)
// an return it in 'neighborOffsets'. Moreover, create a list of flags
// for each BorderType that is 'true' when the corresponding neighbor exists
// in this border situation and return the result in 'neighborExists'.
template <class Shape>
void
makeArrayNeighborhood(ArrayVector<Shape> & neighborOffsets,
ArrayVector<ArrayVector<bool> > & neighborExists,
NeighborhoodType neighborhoodType = DirectNeighborhood)
{
enum { N = Shape::static_size };
neighborOffsets.clear();
if(neighborhoodType == DirectNeighborhood)
{
MakeDirectArrayNeighborhood<N-1>::offsets(neighborOffsets);
}
else
{
Shape point; // represents the center
MakeIndirectArrayNeighborhood<N-1>::offsets(neighborOffsets, point);
}
unsigned int borderTypeCount = 1 << 2*N;
neighborExists.resize(borderTypeCount);
for(unsigned int k=0; k<borderTypeCount; ++k)
{
neighborExists[k].clear();
if(neighborhoodType == DirectNeighborhood)
{
MakeDirectArrayNeighborhood<N-1>::exists(neighborExists[k], k);
}
else
{
MakeIndirectArrayNeighborhood<N-1>::exists(neighborExists[k], k);
}
}
}
} // namespace detail
//@}
} // namespace vigra
#endif // VIGRA_MULTI_SHAPE_HXX
|