Debian Codegraph @

src/filter/twolay0r/twolay0r.cpp

raw symbols SCIP
1 #include "frei0r.hpp"
2
3 #include <algorithm>
4 #include <vector>
5 #include <utility>
6 #include <cassert>
7
8 #include <iostream>
9
10 class twolay0r : public frei0r::filter
11 {
12 static unsigned char grey(unsigned int value)
13 {
14 unsigned char* rgba = reinterpret_cast<unsigned char*>(&value);
15 unsigned char gw= (rgba[0] + rgba[1] + 2*rgba[2])/4;
16 return gw;
17 }
18
19 struct histogram
20 {
21 histogram()
22 : hist(256)
23 {
24 std::fill(hist.begin(),hist.end(),0);
25 }
26
27 void operator()(uint32_t value)
28 {
29 ++hist[grey(value)];
30 }
31
32 std::vector<unsigned int> hist;
33 };
34
35 public:
36 twolay0r(unsigned int width, unsigned int height)
37 {
38 }
39
40 virtual void update(double time,
41 uint32_t* out,
42 const uint32_t* in)
43 {
44 histogram h;
45
46 // create histogramm
47 for (const unsigned int* i=in; i != in + (width*height);++i)
48 h(*i);
49
50 // calc th
51 int th=127;
52 int th_old=0;
53
54 while (th!=th_old)
55 {
56 th_old=th;
57 // calc low
58 double num = 0;
59 double val = 0;
60 for (int i= (int)(th-1); i!= -1; --i)
61 {
62 num += h.hist[i];
63 val += h.hist[i]*i;
64 }
65 unsigned char low = static_cast<unsigned char>(val/num);
66
67 // clac hi
68 num = 0;
69 val = 0;
70 for (unsigned int i=th;i!=256;++i)
71 {
72 num += h.hist[i];
73 val += h.hist[i]*i;
74 }
75 unsigned char hi = static_cast<unsigned char>(val/num);
76
77 th = (low + hi) / 2;
78 }
79
80
81
82 // create b/w image with the th value
83 {
84 uint32_t* outpixel= out;
85 const uint32_t* pixel=in;
86 while(pixel != in+(width*height))
87 {
88 if ( grey(*pixel)<th )
89 *outpixel=0xFF000000;
90 else
91 *outpixel=0xFFFFFFFF;
92 ++outpixel;
93 ++pixel;
94 }
95 }
96 }
97 };
98
99
100 frei0r::construct<twolay0r> plugin("Twolay0r",
101 "dynamic thresholding",
102 "Martin Bayer",
103 0,2);
104