The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
summed_area_tables.h
Go to the documentation of this file.
1 /*
2  * Dune D3D library - Tobias Alexander Franke 2013
3  * For copyright and license see LICENSE
4  * http://www.tobias-franke.eu
5  */
6 
9 #ifndef DUNE_SUMMED_AREA_TABLES
10 #define DUNE_SUMMED_AREA_TABLES
11 
12 #include <cassert>
13 #include <vector>
14 
15 #include <D3D11.h>
16 #include <DirectXMath.h>
17 
18 namespace dune
19 {
20  template<typename T>
21  T luminance(const T& r, const T& g, const T& b)
22  {
23  T v = static_cast<T>(0.2126*r + 0.7152*g + 0.0722*b);
24  return v*v;
25  }
26 
27  struct environment_light
28  {
29  float theta, phi;
30  DirectX::XMFLOAT2 pos;
31  DirectX::XMFLOAT3 flux;
32  };
33 
34  class summed_area_table
35  {
36  protected:
37  int width_, height_;
38  std::vector<float> sat_;
39 
40  float I(int x, int y) const;
41 
42  public:
43  //template<typename T>
44  void create_lum(const BYTE* rgb, size_t width, size_t height, int nc)
45  {
46  assert(nc > 2);
47 
48  width_ = static_cast<int>(width); height_ = static_cast<int>(height);
49 
50  sat_.clear();
51  sat_.resize(width_ * height_);
52 
53  for (int y = 0; y < height_; ++y)
54  for (int x = 0; x < width_; ++x)
55  {
56  size_t i = y*width_ + x;
57 
58  float r = static_cast<float>(rgb[i*nc + 0]);
59  float g = static_cast<float>(rgb[i*nc + 1]);
60  float b = static_cast<float>(rgb[i*nc + 2]);
61 
62  float ixy = luminance(r,g,b);
63 
64  sat_[i] = ixy + I(x-1, y) + I(x, y-1) - I(x-1, y-1);
65  }
66  }
67 
68  int width() const { return width_; }
69  int height() const { return height_; }
70 
79  float sum(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy) const;
80  };
81 
82  struct sat_region
83  {
84  int x_, y_, w_, h_;
85  float sum_;
86  const summed_area_table* sat_;
87 
88  void create(int x, int y, int w, int h, const summed_area_table* sat, float init_sum = -1);
89 
91  void split_w(sat_region& A, sat_region& B) const;
92  void split_w(sat_region& A) const;
93 
95  void split_h(sat_region& A, sat_region& B) const;
96  void split_h(sat_region& A) const;
97 
98  DirectX::XMFLOAT2 centroid() const;
99  };
100 
101  void median_cut(const BYTE* rgba, size_t width, size_t height, size_t n, std::vector<sat_region>& regions, summed_area_table& img);
102  void median_cut(const BYTE* rgba, size_t width, size_t height, size_t n, std::vector<environment_light>& lights);
103 }
104 
105 #endif