Thread: Vapoursynth
View Single Post
Old 5th January 2020, 19:54   #3748  |  Link
feisty2
I'm Siri
 
feisty2's Avatar
 
Join Date: Oct 2012
Location: void
Posts: 2,633
I wrote some syntactic sugar stuff for the C++ API here: https://github.com/IFeelBloated/vapo...ster/sugar.hpp
C++2a support is required (I'm 100% sure C++17 aint cuttin' it), it compiles with "-std=c++2a" for GCC10

you can use
Code:
auto MakePlane(auto Pointer, auto Width, auto Height, auto PaddingPolicy)
to access the plane in a 2-dimensional style, out-of-bound access (automatic padding) is allowed for the source plane, but not allowed for the destination plane.

I'll give you a concrete example, to write a 3x3 box blur, you would do the following:
Code:
auto srcp = reinterpret_cast<const float *>(srcp8);
auto dstp = reinterpret_cast<float *>(dstp8);

auto padded_src = reinterpret_cast<float *>(malloc((width+2) * (height+2) * sizeof(float)));
for (auto y : Range{ height })
     std::memcpy(padded_src + (y+1) * (width+2) + 1, srcp + y * width, width * sizeof(float));

// lots of code here to deal with padding for "padded_src"

auto gc_addr = padded_src;
padded_src += (width + 2) + 1;

for (auto y : Range{ height })
     for (auto x : Range{ width }) {
             auto above = padded_src - (width + 2);
             auto below = padded_src + (width + 2);
             dstp[x] = (above[x-1] + above[x] + above[x+1] + padded_src[x-1] + padded_src[x] + padded_src[x+1] + below[x-1] + below[x] + below[x+1]) / 9;
             dstp += width;
             padded_src += width + 2;
     }

free(gc_addr);
with "sugar.hpp", that huge pile of nuisance is equivalent to simply
Code:
auto srcp = MakePlane<const float>(srcp8, width, height, Repeat);
auto dstp = MakePlane<float>(dstp8, width, height, Zero);

for (auto y : Range{ height })
     for (auto x : Range{ width })
             dstp[y][x] = (srcp[y-1][x-1] + srcp[y-1][x] + srcp[y-1][x+1] + srcp[y][x-1] + srcp[y][x] + srcp[y][x+1] + srcp[y+1][x-1] + srcp[y+1][x] + srcp[y+1][x+1]) / 9;
             // possible out-of-bound access like "srcp[y-1][x-1]" is automatically handled here with the given padding policy, you got nothing to worry about
The header includes 3 pre-defined padding policies
Code:
Zero
Repeat (or simply called "pad" on this forum)
Reflect
however, you can also define your own padding policy by completing the following function and pass it to "MakePlane()"
Code:
auto PaddingFunction = [](auto Canvas, auto Width, auto Height, auto y, auto x) {
      ...
};
note that the "PaddingPolicy" argument has no effect on "dstp" as no padding is applied here.

you are welcome to leave a comment if you have any suggestions or ideas to further improve this.

Last edited by feisty2; 6th January 2020 at 11:45.
feisty2 is offline   Reply With Quote