1 module oclcv.resize;
2 
3 import oclcv.clcore;
4 import dplug.core.nogc;
5 
6 final class Resize3(string method) {
7     // available methods : resize_linear, resize_bicubic
8 public:
9 @nogc nothrow:
10     this(int srcHeight, int srcWidth, int dstWidth, int dstHeight, CLContext ctx){
11         srcHeight_ = srcHeight; srcWidth_= srcWidth;
12         dstHeight_ = dstHeight; dstWidth_= dstWidth;
13         
14         if(!initialize(ctx)){
15             import core.stdc.stdlib, core.stdc.stdio;
16             printf("Problem initializing the OpenCL kernel %s", __FILE__.ptr);
17             exit(-1);
18         }
19     }
20 
21     ~this(){
22         destroyFree(prog_);
23     }
24 
25     bool initialize(CLContext ctx){
26         if(!ctx)
27             return false;
28         context_ = ctx;
29         
30         prog_ = mallocNew!CLProgram(CTKernel.KRESIZE, context_);
31         resize_kernel = prog_.getKernel(method);
32         
33         return true;
34     }
35 
36     CLBuffer run(CLBuffer d_src_3){
37         debug _assert(d_src_3.metaData.dataType == UBYTE, "Input type must be ubyte"); 
38         debug _assert(d_src_3.metaData.numberOfChannels == 3, "Input's channel count must be 3");
39 
40         CLBuffer d_out = mallocNew!CLBuffer(context_, BufferMeta(UBYTE, dstHeight_, dstWidth_, 3));
41 
42         struct _int2 {int x, y;}
43         auto srcSize = _int2(srcWidth_, srcHeight_);
44         auto dstSize = _int2(dstWidth_, dstHeight_);
45         resize_kernel.setArgs(d_src_3, d_out, srcSize, dstSize);
46         
47         _resize();
48 
49         return d_out;
50     }
51 
52     void _resize(){
53         import std.algorithm.comparison : max;
54         resize_kernel.launch(0, GridDim((dstWidth_ + 16 - 1)/16, (dstHeight_ + 16 - 1)/16), BlockDim(16,16));
55         //size_t[3] sz = [max(srcWidth_, dstWidth_), max(srcHeight_, dstHeight_), 1];
56         //resize_kernel.launch(0, null, sz.ptr, null);
57         context_.finish(0);
58     }
59 
60 private:
61     int srcWidth_, srcHeight_;
62     int dstWidth_, dstHeight_;
63     
64     CLContext context_;
65     CLProgram prog_;
66 
67     CLKernel resize_kernel;
68 }