libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNSummedAreaTable.h
Go to the documentation of this file.
1 /* Copyright 2013-2014 INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNSummedAreaTable.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNSummedAreaTable_HEADER
23 #define CRNSummedAreaTable_HEADER
24 
25 #include <vector>
26 #include <CRNGeometry/CRNRect.h>
27 #include <CRNException.h>
28 
29 namespace crn
30 {
31  /****************************************************************************/
41  template<typename T> class SummedAreaTable
42  {
43  public:
45  inline SummedAreaTable(size_t w, size_t h):width(w),height(h),data(w * h, T(0)) { }
46 
47  SummedAreaTable(const SummedAreaTable&) = default;
48  SummedAreaTable(SummedAreaTable&&) = default;
49  SummedAreaTable& operator=(const SummedAreaTable&) = default;
51 
53  inline T GetValue(size_t x, size_t y) const { return data[x + y * width]; }
55  inline void SetValue(size_t x, size_t y, T val) { data[x + y * width] = val; }
56 
62  inline T GetSum(const crn::Rect &r) const
63  {
64  if ((r.GetLeft() < 0) || (r.GetTop() < 0) || (r.GetRight() >= int(width)) || (r.GetBottom() >= int(height)))
65  throw ExceptionDimension("Rectangle out of bounds");
66  return GetSum(r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
67  }
68 
77  inline T GetSum(size_t x1, size_t y1, size_t x2, size_t y2) const
78  {
79  if ((x1 == 0) && (y1 == 0))
80  return data[x2 + y2 * width];
81  else if (x1 == 0)
82  return T(data[x2 + y2 * width] - data[x2 + (y1 - 1) * width]);
83  else if (y1 == 0)
84  return T(data[x2 + y2 * width] - data[x1 - 1 + y2 * width]);
85  else
86  return T(data[x2 + y2 * width] + data[x1 - 1 + (y1 - 1) * width] - data[(x1 - 1) + y2 * width] - data[x2 + (y1 - 1) * width]);
87  }
88 
89  private:
90  size_t width, height;
91  std::vector<T> data;
92  };
93 }
94 
95 #endif
int GetBottom() const
Returns the bottommost coordinate.
Definition: CRNRect.h:111
T GetValue(size_t x, size_t y) const
Gets the value of a bin.
int GetTop() const
Returns the topmost coordinate.
Definition: CRNRect.h:107
int GetLeft() const
Returns the leftmost coordinate.
Definition: CRNRect.h:99
SummedAreaTable(size_t w, size_t h)
Constructor.
void SetValue(size_t x, size_t y, T val)
Alters the value of a bin.
A dimension error.
Definition: CRNException.h:119
A summed area table.
T GetSum(const crn::Rect &r) const
Gets the sum of the original pixels in a rectangle.
T GetSum(size_t x1, size_t y1, size_t x2, size_t y2) const
Gets the sum of the original pixels in a rectangle.
int GetRight() const
Returns the rightmost coordinate.
Definition: CRNRect.h:103
SummedAreaTable & operator=(const SummedAreaTable &)=default
A rectangle class.
Definition: CRNRect.h:46