LrSize=2*D2;//TODO: Check: do we need this value or not?
Cbuf=alignedBuf;
Sbuf=Cbuf+CSBufSize;
}
CV_Assert(D%16==0);//TODO: Are you sure? By the way, why not 8?
// NR - the number of directions. the loop on x below that computes Lr assumes that NR == 8.
// if you change NR, please, modify the loop as well.
intD2=D+16;//4e: Somewhere in code we need d+1, so D+1. One of simplest solutuons is increasing D-dimension on 1. But 1 is 16, when storage should be aligned.
// the number of L_r(.,.) and min_k L_r(.,.) lines in the buffer:
// for 8-way dynamic programming we need the current row and
// the previous row, i.e. 2 rows in total
constintNLR=2;//4e: We assume, that we need one or more previous steps in our linear dynamic(one right here).
constintLrBorder=NLR-1;//4e: for simplification of calculations we need border for taking previous dynamic solutions.
// for each possible stereo match (img1(x,y) <=> img2(x-d,y))
// we keep pixel difference cost (C) and the summary cost over NR directions (S).
// we also keep all the partial costs for the previous line L_r(x,d) and also min_k L_r(x, k)
size_tcostBufSize=width1*D;
size_tCSBufSize=costBufSize*height;
size_tminLrSize=(width1+LrBorder*2),LrSize=minLrSize*D2;//TODO: We don't need LrBorder for vertical passes and we don't need Lr buffer for horizontal passes.
inthsumBufNRows=SH2*2+2;
size_ttotalBufSize=(LrSize+minLrSize)*NLR*sizeof(CostType)+// minLr[] and Lr[]
costBufSize*hsumBufNRows*sizeof(CostType)+// hsumBuf //4e: TODO: Why we should increase sum window height one more time?
CSBufSize*2*sizeof(CostType)+// C, S //4e: C is Block sum of costs, S is multidirectional dynamic sum with same size
CostType*disp2cost=hsumBuf+costBufSize*hsumBufNRows+(LrSize+minLrSize)*NLR;//4e: It is containers for backwards disparity, made by S[d] too, but with other method
DispType*disp2ptr=(DispType*)(disp2cost+width);
// add P2 to every C(x,y). it saves a few operations in the inner loops
memset(Lr-D2*LrBorder-8,0,D2*LrBorder*sizeof(CostType));//4e: To understand this "8" shifts and how they could work it's simpler to imagine pixel dislocation in memory
memset(Lr+width1*D2-8,0,D2*LrBorder*sizeof(CostType));//4e: ...00000000|D2-16 of real costs value(and some of them are zeroes too)|00000000...
// we keep pixel difference cost (C) and the summary cost over NR directions (S).
// we also keep all the partial costs for the previous line L_r(x,d) and also min_k L_r(x, k)
size_tcostBufSize=width1*D;
size_tCSBufSize=costBufSize*height;//4e: For HH mode it's better to keep whole array of costs.
size_tminLrSize=(width1+LrBorder*2),LrSize=minLrSize*D2;//4e: TODO: Understand why NR2 per pass instead od NR2/2 (Probably, without any reason. That doesn't make code wrong)
size_tCSBufSize=costBufSize*height;
size_tminLrSize=(width1+LrBorder*2),LrSize=minLrSize*D2;//TODO: We don't need LrBorder for vertical passes and we don't need Lr buffer for horizontal passes.
inthsumBufNRows=SH2*2+2;
size_ttotalBufSize=(LrSize+minLrSize)*NLR*sizeof(CostType)+// minLr[] and Lr[]
costBufSize*(hsumBufNRows+1)*sizeof(CostType)+// hsumBuf, pixdiff //4e: TODO: Why we should increase sum window height one more time?
CSBufSize*2*sizeof(CostType)+// C, S //4e: C is Block sum of costs, S is multidirectional dynamic sum with same size
width*16*img1.channels()*sizeof(PixType)+// temp buffer for computing per-pixel cost //4e: It is needed for calcPixelCostBT function, as "buffer" value
if(y>0)//4e: We calculate horizontal sums and forming full block sums for y coord by adding this horsums to previous line's sums and subtracting stored lowest
{//4e: horsum in hsumBuf. Exception is case y=0, where we need many iterations per lines to create full blocking sum.
for(k=0;k<width1*D;k++)//4e: only on first pass, so it keep old information, don't be confused
S[k]=0;
}
// clear the left and the right borders
memset(Lr[0]-D2*LrBorder-8,0,D2*LrBorder*sizeof(CostType));//4e: To understand this "8" shifts and how they could work it's simpler to imagine pixel dislocation in memory
memset(Lr[0]+width1*D2-8,0,D2*LrBorder*sizeof(CostType));//4e: ...00000000|D2-16 of real costs value(and some of them are zeroes too)|00000000...
memset(Lr-D2*LrBorder-8,0,D2*LrBorder*sizeof(CostType));//4e: To understand this "8" shifts and how they could work it's simpler to imagine pixel dislocation in memory
memset(Lr+width1*D2-8,0,D2*LrBorder*sizeof(CostType));//4e: ...00000000|D2-16 of real costs value(and some of them are zeroes too)|00000000...