| 1 | #cxx . . $ `<file>/src/filter/twolay0r/twolay0r.cpp`/include "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/ |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <vector> |
| 5 | #include <utility> |
| 6 | #include <cassert> |
| 7 | |
| 8 | #include <iostream> |
| 9 | |
| 10 | class twolay0rcxx . . $ twolay0r# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter# |
| 11 | { |
| 12 | static unsigned char greycxx . . $ twolay0r#grey(32407b39b2dfce37).(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 histogramcxx . . $ twolay0r#histogram# |
| 20 | { |
| 21 | histogramcxx . . $ twolay0r#histogram#histogram(49f6e7a06ebc5aa8).() |
| 22 | : histcxx . . $ twolay0r#histogram#hist.(256) |
| 23 | { |
| 24 | stdcxx . . $ std/::fillcxx . . $ std/fill(4a9bfaed88eccda7).(histcxx . . $ twolay0r#histogram#hist..begincxx . . $ std/vector#begin(33b10a496a38335b).(),histcxx . . $ twolay0r#histogram#hist..endcxx . . $ std/vector#end(33b10a496a38335b).(),0); |
| 25 | } |
| 26 | |
| 27 | void operatorcxx . . $ twolay0r#histogram#`operator()`(cb2f890c2fdad230).()(uint32_t value) |
| 28 | { |
| 29 | ++histcxx . . $ twolay0r#histogram#hist.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).greycxx . . $ twolay0r#grey(32407b39b2dfce37).(value)]; |
| 30 | } |
| 31 | |
| 32 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<unsigned int> histcxx . . $ twolay0r#histogram#hist.; |
| 33 | }; |
| 34 | |
| 35 | public: |
| 36 | twolay0rcxx . . $ twolay0r#twolay0r(b4d55b1286ff73e8).(unsigned int width, unsigned int height) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | virtual void updatecxx . . $ twolay0r#update(da7b4a17ceb4e697).(double time, |
| 41 | uint32_t* out, |
| 42 | const uint32_t* in) |
| 43 | { |
| 44 | histogramcxx . . $ twolay0r#histogram# hcxx . . $ twolay0r#histogram#histogram(49f6e7a06ebc5aa8).; |
| 45 | |
| 46 | // create histogramm |
| 47 | for (const unsigned int* i=in; i != in + (widthcxx . . $ frei0r/fx#width.*heightcxx . . $ frei0r/fx#height.);++i) |
| 48 | h(cxx . . $ twolay0r#histogram#`operator()`(cb2f890c2fdad230).*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.histcxx . . $ twolay0r#histogram#hist.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).i]; |
| 63 | val += h.histcxx . . $ twolay0r#histogram#hist.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).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.histcxx . . $ twolay0r#histogram#hist.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).i]; |
| 73 | val += h.histcxx . . $ twolay0r#histogram#hist.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).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+(widthcxx . . $ frei0r/fx#width.*heightcxx . . $ frei0r/fx#height.)) |
| 87 | { |
| 88 | if ( greycxx . . $ twolay0r#grey(32407b39b2dfce37).(*pixel)<th ) |
| 89 | *outpixel=0xFF000000; |
| 90 | else |
| 91 | *outpixel=0xFFFFFFFF; |
| 92 | ++outpixel; |
| 93 | ++pixel; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<twolay0rcxx . . $ twolay0r#> plugincxx . . $ plugin.("Twolay0r"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 101 | "dynamic thresholding"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 102 | "Martin Bayer"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 103 | 0,2); |
| 104 |