From acd2d20ef1e80804117b9ce4d0db9111466767be Mon Sep 17 00:00:00 2001 From: Ingo Date: Wed, 15 May 2013 13:10:56 +0200 Subject: [PATCH] Amaze optimization, Issue 1835 --- rtengine/amaze_demosaic_RT.cc | 450 +++++++++++++++++++--------------- 1 file changed, 259 insertions(+), 191 deletions(-) diff --git a/rtengine/amaze_demosaic_RT.cc b/rtengine/amaze_demosaic_RT.cc index 3806e2164..80fe733cb 100644 --- a/rtengine/amaze_demosaic_RT.cc +++ b/rtengine/amaze_demosaic_RT.cc @@ -29,10 +29,14 @@ #include "rt_math.h" #include "../rtgui/multilangmgr.h" #include "procparams.h" +#include "sleef.c" using namespace rtengine; void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { +// clock_t t1,t2; +// t1 = clock(); + #define HCLIP(x) x //is this still necessary??? //min(clip_pt,x) @@ -42,7 +46,8 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { const float clip_pt = 1/initialGain; #define TS 512 // Tile size; the image is processed in square tiles to lower memory requirements and facilitate multi-threading - +#define TSH 256 +#define TS6 500 // local variables @@ -79,7 +84,14 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { plistener->setProgressStr (Glib::ustring::compose(M("TP_RAW_DMETHOD_PROGRESSBAR"), RAWParams::methodstring[RAWParams::amaze])); plistener->setProgress (0.0); } - +struct s_mp { + float m; + float p; +}; +struct s_hv { + float h; + float v; +}; #pragma omp parallel { //position of top/left corner of the tile @@ -118,71 +130,82 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { float (*delm); // diagonal interpolation of R+B float (*rbint); + s_hv (*Dgrb2); // horizontal curvature of interpolated G (used to refine interpolation in Nyquist texture regions) - float (*Dgrbh2); +// float (*Dgrbh2); // vertical curvature of interpolated G - float (*Dgrbv2); +// float (*Dgrbv2); // difference between up/down interpolations of G float (*dgintv); // difference between left/right interpolations of G float (*dginth); // diagonal (plus) color difference R-B or G1-G2 - float (*Dgrbp1); +// float (*Dgrbp1); // diagonal (minus) color difference R-B or G1-G2 - float (*Dgrbm1); +// float (*Dgrbm1); + s_mp (*Dgrbsq1); // square of diagonal color difference - float (*Dgrbpsq1); +// float (*Dgrbpsq1); // square of diagonal color difference - float (*Dgrbmsq1); +// float (*Dgrbmsq1); // tile raw data float (*cfa); // relative weight for combining plus and minus diagonal interpolations float (*pmwt); + // interpolated color difference R-B in minus and plus direction + s_mp (*rb); // interpolated color difference R-B in plus direction - float (*rbp); +// float (*rbp); // interpolated color difference R-B in minus direction - float (*rbm); +// float (*rbm); // nyquist texture flag 1=nyquist, 0=not nyquist - int (*nyquist); - + char (*nyquist); +#define CLF 1 // assign working space - buffer = (char *) malloc((32*sizeof(float)+sizeof(int))*TS*TS); + buffer = (char *) malloc(29*sizeof(float)*TS*TS - sizeof(float)*TS*TSH + sizeof(char)*TS*TSH+23*CLF*64); + char *data; + data = (char*)( ( uintptr_t(buffer) + uintptr_t(63)) / 64 * 64); + + //merror(buffer,"amaze_interpolate()"); //memset(buffer,0,(34*sizeof(float)+sizeof(int))*TS*TS); // rgb array - rgb = (float (*)[3]) buffer; //pointers to array - delh = (float (*)) (buffer + 3*sizeof(float)*TS*TS); - delv = (float (*)) (buffer + 4*sizeof(float)*TS*TS); - delhsq = (float (*)) (buffer + 5*sizeof(float)*TS*TS); - delvsq = (float (*)) (buffer + 6*sizeof(float)*TS*TS); - dirwts = (float (*)[2]) (buffer + 7*sizeof(float)*TS*TS); - vcd = (float (*)) (buffer + 9*sizeof(float)*TS*TS); - hcd = (float (*)) (buffer + 10*sizeof(float)*TS*TS); - vcdalt = (float (*)) (buffer + 11*sizeof(float)*TS*TS); - hcdalt = (float (*)) (buffer + 12*sizeof(float)*TS*TS); - cddiffsq = (float (*)) (buffer + 13*sizeof(float)*TS*TS); - hvwt = (float (*)) (buffer + 14*sizeof(float)*TS*TS); - Dgrb = (float (*)[2]) (buffer + 15*sizeof(float)*TS*TS); - delp = (float (*)) (buffer + 17*sizeof(float)*TS*TS); - delm = (float (*)) (buffer + 18*sizeof(float)*TS*TS); - rbint = (float (*)) (buffer + 19*sizeof(float)*TS*TS); - Dgrbh2 = (float (*)) (buffer + 20*sizeof(float)*TS*TS); - Dgrbv2 = (float (*)) (buffer + 21*sizeof(float)*TS*TS); - dgintv = (float (*)) (buffer + 22*sizeof(float)*TS*TS); - dginth = (float (*)) (buffer + 23*sizeof(float)*TS*TS); - Dgrbp1 = (float (*)) (buffer + 24*sizeof(float)*TS*TS); - Dgrbm1 = (float (*)) (buffer + 25*sizeof(float)*TS*TS); - Dgrbpsq1 = (float (*)) (buffer + 26*sizeof(float)*TS*TS); - Dgrbmsq1 = (float (*)) (buffer + 27*sizeof(float)*TS*TS); - cfa = (float (*)) (buffer + 28*sizeof(float)*TS*TS); - pmwt = (float (*)) (buffer + 29*sizeof(float)*TS*TS); - rbp = (float (*)) (buffer + 30*sizeof(float)*TS*TS); - rbm = (float (*)) (buffer + 31*sizeof(float)*TS*TS); - - nyquist = (int (*)) (buffer + 32*sizeof(int)*TS*TS); + rgb = (float (*)[3]) data; //pointers to array + delh = (float (*)) (data + 3*sizeof(float)*TS*TS+1*CLF*64); + delv = (float (*)) (data + 4*sizeof(float)*TS*TS+2*CLF*64); + delhsq = (float (*)) (data + 5*sizeof(float)*TS*TS+3*CLF*64); + delvsq = (float (*)) (data + 6*sizeof(float)*TS*TS+4*CLF*64); + dirwts = (float (*)[2]) (data + 7*sizeof(float)*TS*TS+5*CLF*64); + vcd = (float (*)) (data + 9*sizeof(float)*TS*TS+6*CLF*64); + hcd = (float (*)) (data + 10*sizeof(float)*TS*TS+7*CLF*64); + vcdalt = (float (*)) (data + 11*sizeof(float)*TS*TS+8*CLF*64); + hcdalt = (float (*)) (data + 12*sizeof(float)*TS*TS+9*CLF*64); + cddiffsq = (float (*)) (data + 13*sizeof(float)*TS*TS+10*CLF*64); + hvwt = (float (*)) (data + 14*sizeof(float)*TS*TS+11*CLF*64); //compressed 0.5 MB + Dgrb = (float (*)[2]) (data + 15*sizeof(float)*TS*TS - sizeof(float)*TS*TSH+12*CLF*64); + delp = (float (*)) (data + 17*sizeof(float)*TS*TS - sizeof(float)*TS*TSH+13*CLF*64); // compressed 0.5 MB + delm = (float (*)) (data + 17*sizeof(float)*TS*TS+14*CLF*64); // compressed 0.5 MB + rbint = (float (*)) (data + 18*sizeof(float)*TS*TS - sizeof(float)*TS*TSH+15*CLF*64); // compressed 0.5 MB + Dgrb2 = (s_hv (*)) (data + 18*sizeof(float)*TS*TS+16*CLF*64); // compressed 1.0 MB +// Dgrbh2 = (float (*)) (data + 19*sizeof(float)*TS*TS); +// Dgrbv2 = (float (*)) (data + 20*sizeof(float)*TS*TS); + dgintv = (float (*)) (data + 19*sizeof(float)*TS*TS+17*CLF*64); + dginth = (float (*)) (data + 20*sizeof(float)*TS*TS+18*CLF*64); +// Dgrbp1 = (float (*)) (data + 23*sizeof(float)*TS*TS); 1.0 MB +// Dgrbm1 = (float (*)) (data + 23*sizeof(float)*TS*TS); 1.0 MB + Dgrbsq1 = (s_mp (*)) (data + 21*sizeof(float)*TS*TS+19*CLF*64); // compressed 1.0 MB +// Dgrbpsq1 = (float (*)) (data + 23*sizeof(float)*TS*TS); +// Dgrbmsq1 = (float (*)) (data + 24*sizeof(float)*TS*TS); + cfa = (float (*)) (data + 22*sizeof(float)*TS*TS+20*CLF*64); + pmwt = (float (*)) (data + 23*sizeof(float)*TS*TS+21*CLF*64); // compressed 0.5 MB + rb = (s_mp (*)) (data + 24*sizeof(float)*TS*TS - sizeof(float)*TS*TSH+22*CLF*64); // compressed 1.0 MB +// rbp = (float (*)) (data + 30*sizeof(float)*TS*TS); +// rbm = (float (*)) (data + 31*sizeof(float)*TS*TS); + nyquist = (char (*)) (data + 25*sizeof(float)*TS*TS - sizeof(float)*TS*TSH+23*CLF*64); //compressed 0.875 MB +#undef CLF // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /*double dt; @@ -224,6 +247,8 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { #pragma omp for schedule(dynamic) collapse(2) nowait for (top=winy-16; top < winy+height; top += TS-32) for (left=winx-16; left < winx+width; left += TS-32) { + memset(nyquist, 0, sizeof(char)*TS*TSH); + memset(rbint, 0, sizeof(float)*TS*TSH); //location of tile bottom edge int bottom = min(top+TS,winy+height+16); //location of tile right edge @@ -247,7 +272,7 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { //dummy indices int i, j; // +1 or -1 - int sgn; +// int sgn; //color ratios in up/down/left/right directions float cru, crd, crl, crr; @@ -263,23 +288,25 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { float Ginthar, Ginthha, Gintvar, Gintvha; //color difference (G-R or G-B) variance in up/down/left/right directions float Dgrbvvaru, Dgrbvvard, Dgrbhvarl, Dgrbhvarr; + float uave, dave, lave, rave; + //color difference variances in vertical and horizontal directions float vcdvar, hcdvar, vcdvar1, hcdvar1, hcdaltvar, vcdaltvar; //adaptive interpolation weight using variance of color differences - float varwt; + float varwt; // 639 - 644 //adaptive interpolation weight using difference of left-right and up-down G interpolations - float diffwt; + float diffwt; // 640 - 644 //alternative adaptive weight for combining horizontal/vertical interpolations - float hvwtalt; + float hvwtalt; // 745 - 748 //temporary variables for combining interpolation weights at R and B sites - float vo, ve; +// float vo, ve; //interpolation of G in four directions float gu, gd, gl, gr; //variance of G in vertical/horizontal directions float gvarh, gvarv; //Nyquist texture test - float nyqtest; + float nyqtest; // 658 - 681 //accumulators for Nyquist texture interpolation float sumh, sumv, sumsqh, sumsqv, areawt; @@ -290,9 +317,9 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { //adaptive weights for combining diagonal interpolations float wtse, wtnw, wtsw, wtne; //alternate weight for combining diagonal interpolations - float pmwtalt; + float pmwtalt; // 885 - 888 //variance of R-B in plus/minus directions - float rbvarp, rbvarm; + float rbvarm; // 843 - 848 @@ -402,32 +429,37 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (rr=1; rr < rr1-1; rr++) for (cc=1, indx=(rr)*TS+cc; cc < cc1-1; cc++, indx++) { - delh[indx] = fabs(cfa[indx+1]-cfa[indx-1]); - delv[indx] = fabs(cfa[indx+v1]-cfa[indx-v1]); + delh[indx] = fabsf(cfa[indx+1]-cfa[indx-1]); + delv[indx] = fabsf(cfa[indx+v1]-cfa[indx-v1]); delhsq[indx] = SQR(delh[indx]); delvsq[indx] = SQR(delv[indx]); - delp[indx] = fabs(cfa[indx+p1]-cfa[indx-p1]); - delm[indx] = fabs(cfa[indx+m1]-cfa[indx-m1]); - +// delp[indx] = fabsf(cfa[indx+p1]-cfa[indx-p1]); +// delm[indx] = fabsf(cfa[indx+m1]-cfa[indx-m1]); } for (rr=2; rr < rr1-2; rr++) for (cc=2,indx=(rr)*TS+cc; cc < cc1-2; cc++, indx++) { - - dirwts[indx][0] = eps+delv[indx+v1]+delv[indx-v1]+delv[indx];//+fabs(cfa[indx+v2]-cfa[indx-v2]); + dirwts[indx][0] = eps+delv[indx+v1]+delv[indx-v1]+delv[indx];//+fabsf(cfa[indx+v2]-cfa[indx-v2]); //vert directional averaging weights - dirwts[indx][1] = eps+delh[indx+1]+delh[indx-1]+delh[indx];//+fabs(cfa[indx+2]-cfa[indx-2]); + dirwts[indx][1] = eps+delh[indx+1]+delh[indx-1]+delh[indx];//+fabsf(cfa[indx+2]-cfa[indx-2]); //horizontal weights - if (FC(rr,cc)&1) { - //for later use in diagonal interpolation - //Dgrbp1[indx]=2*cfa[indx]-(cfa[indx-p1]+cfa[indx+p1]); - //Dgrbm1[indx]=2*cfa[indx]-(cfa[indx-m1]+cfa[indx+m1]); - Dgrbpsq1[indx]=(SQR(cfa[indx]-cfa[indx-p1])+SQR(cfa[indx]-cfa[indx+p1])); - Dgrbmsq1[indx]=(SQR(cfa[indx]-cfa[indx-m1])+SQR(cfa[indx]-cfa[indx+m1])); - } } + for (rr=6; rr < rr1-6; rr++) + for (cc=6+(FC(rr,2)&1), indx=(rr)*TS+cc; cc < cc1-6; cc+=2, indx+=2) { + delp[indx>>1] = fabsf(cfa[indx+p1]-cfa[indx-p1]); + delm[indx>>1] = fabsf(cfa[indx+m1]-cfa[indx-m1]); + } + + for (rr=6; rr < rr1-6; rr++) + for (cc=6+(FC(rr,1)&1),indx=(rr)*TS+cc; cc < cc1-6; cc+=2, indx+=2) { + Dgrbsq1[indx>>1].p=(SQR(cfa[indx]-cfa[indx-p1])+SQR(cfa[indx]-cfa[indx+p1])); + Dgrbsq1[indx>>1].m=(SQR(cfa[indx]-cfa[indx-m1])+SQR(cfa[indx]-cfa[indx+m1])); + } + + + //t2_init += clock()-t1_init; // end of tile initialization // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -438,13 +470,13 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (rr=4; rr 0.8*clip_pt || Gintvha > 0.8*clip_pt || Ginthha > 0.8*clip_pt) { //use HA if highlights are (nearly) clipped guar=guha; gdar=gdha; glar=glha; grar=grha; @@ -529,7 +579,7 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { if (Gintv > clip_pt) vcd[indx]=-ULIM(Gintv,cfa[indx-v1],cfa[indx+v1])+cfa[indx]; //if (Ginth > pre_mul[c]) hcd[indx]=-ULIM(Ginth,cfa[indx-1],cfa[indx+1])+cfa[indx];//for dcraw implementation //if (Gintv > pre_mul[c]) vcd[indx]=-ULIM(Gintv,cfa[indx-v1],cfa[indx+v1])+cfa[indx]; - + } else {//R or B site Ginth = hcd[indx]+cfa[indx];//interpolated G @@ -556,9 +606,10 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { if (Gintv > clip_pt) vcd[indx]=ULIM(Gintv,cfa[indx-v1],cfa[indx+v1])-cfa[indx]; //if (Ginth > pre_mul[c]) hcd[indx]=ULIM(Ginth,cfa[indx-1],cfa[indx+1])-cfa[indx];//for dcraw implementation //if (Gintv > pre_mul[c]) vcd[indx]=ULIM(Gintv,cfa[indx-v1],cfa[indx+v1])-cfa[indx]; + cddiffsq[indx] = SQR(vcd[indx]-hcd[indx]); } - cddiffsq[indx] = SQR(vcd[indx]-hcd[indx]); +// cddiffsq[indx] = SQR(vcd[indx]-hcd[indx]); } for (rr=6; rr0 && fabs(0.5-diffwt)0 && fabsf(0.5-diffwt)>1]=varwt;} else {hvwt[indx>>1]=diffwt;} //hvwt[indx]=varwt; } @@ -634,19 +685,19 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { delhsq[indx-p2]+delvsq[indx-p2]+delhsq[indx+m2]+delvsq[indx+m2])); - if (nyqtest>0) {nyquist[indx]=1;}//nyquist=1 for nyquist region + if (nyqtest>0) {nyquist[indx>>1]=1;}//nyquist=1 for nyquist region } - + unsigned int nyquisttemp; for (rr=8; rr>1]+nyquist[(indx-m1)>>1]+nyquist[(indx+p1)>>1]+ + nyquist[(indx-2)>>1]+nyquist[indx>>1]+nyquist[(indx+2)>>1]+ + nyquist[(indx-p1)>>1]+nyquist[(indx+m1)>>1]+nyquist[(indx+v2)>>1]); //if most of your neighbors are named Nyquist, it's likely that you're one too - if (areawt>4) nyquist[indx]=1; + if (nyquisttemp>4) nyquist[indx>>1]=1; //or not - if (areawt<4) nyquist[indx]=0; + if (nyquisttemp<4) nyquist[indx>>1]=0; } //t2_nyqtest += clock() - t1_nyqtest; @@ -662,7 +713,7 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (rr=8; rr>1]) { // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // area interpolation @@ -670,19 +721,19 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (i=-6; i<7; i+=2) for (j=-6; j<7; j+=2) { indx1=(rr+i)*TS+cc+j; - if (nyquist[indx1]) { - sumh += cfa[indx1]-0.5*(cfa[indx1-1]+cfa[indx1+1]); - sumv += cfa[indx1]-0.5*(cfa[indx1-v1]+cfa[indx1+v1]); - sumsqh += 0.5*(SQR(cfa[indx1]-cfa[indx1-1])+SQR(cfa[indx1]-cfa[indx1+1])); - sumsqv += 0.5*(SQR(cfa[indx1]-cfa[indx1-v1])+SQR(cfa[indx1]-cfa[indx1+v1])); + if (nyquist[indx1>>1]) { + sumh += cfa[indx1]-xdiv2f(cfa[indx1-1]+cfa[indx1+1]); + sumv += cfa[indx1]-xdiv2f(cfa[indx1-v1]+cfa[indx1+v1]); + sumsqh += xdiv2f(SQR(cfa[indx1]-cfa[indx1-1])+SQR(cfa[indx1]-cfa[indx1+1])); + sumsqv += xdiv2f(SQR(cfa[indx1]-cfa[indx1-v1])+SQR(cfa[indx1]-cfa[indx1+v1])); areawt +=1; } } //horizontal and vertical color differences, and adaptive weight - hcdvar=epssq+fabs(areawt*sumsqh-sumh*sumh); - vcdvar=epssq+fabs(areawt*sumsqv-sumv*sumv); - hvwt[indx]=hcdvar/(vcdvar+hcdvar); + hcdvar=epssq+fabsf(areawt*sumsqh-sumh*sumh); + vcdvar=epssq+fabsf(areawt*sumsqv-sumv*sumv); + hvwt[indx>>1]=hcdvar/(vcdvar+hcdvar); // end of area interpolation // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -698,24 +749,26 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (cc=8+(FC(rr,2)&1),indx=rr*TS+cc; cc>1]+hvwt[(indx+p1)>>1]+hvwt[(indx-p1)>>1]+hvwt[(indx+m1)>>1],2); +// hvwtalt = 0.25*(hvwt[(indx-m1)>>1]+hvwt[(indx+p1)>>1]+hvwt[(indx-p1)>>1]+hvwt[(indx+m1)>>1]); +// vo=fabsf(0.5-hvwt[indx>>1]); +// ve=fabsf(0.5-hvwtalt); + if (fabsf(0.5-hvwt[indx>>1])>1]=hvwtalt;}//a better result was obtained from the neighbors +// if (vo>1]=hvwtalt;}//a better result was obtained from the neighbors - Dgrb[indx][0] = (hcd[indx]*(1.0f-hvwt[indx]) + vcd[indx]*hvwt[indx]);//evaluate color differences + Dgrb[indx][0] = (hcd[indx]*(1.0f-hvwt[indx>>1]) + vcd[indx]*hvwt[indx>>1]);//evaluate color differences //if (hvwt[indx]<0.5) Dgrb[indx][0]=hcd[indx]; //if (hvwt[indx]>0.5) Dgrb[indx][0]=vcd[indx]; rgb[indx][1] = cfa[indx] + Dgrb[indx][0];//evaluate G (finally!) //local curvature in G (preparation for nyquist refinement step) - if (nyquist[indx]) { - Dgrbh2[indx] = SQR(rgb[indx][1] - 0.5*(rgb[indx-1][1]+rgb[indx+1][1])); - Dgrbv2[indx] = SQR(rgb[indx][1] - 0.5*(rgb[indx-v1][1]+rgb[indx+v1][1])); + if (nyquist[indx>>1]) { + Dgrb2[indx>>1].h = SQR(rgb[indx][1] - xdiv2f(rgb[indx-1][1]+rgb[indx+1][1])); + Dgrb2[indx>>1].v = SQR(rgb[indx][1] - xdiv2f(rgb[indx-v1][1]+rgb[indx+v1][1])); } else { - Dgrbh2[indx] = Dgrbv2[indx] = 0; + Dgrb2[indx>>1].h = Dgrb2[indx>>1].v = 0; } } @@ -730,16 +783,16 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (rr=8; rr>1]) { //local averages (over Nyquist pixels only) of G curvature squared - gvarh = epssq + (gquinc[0]*Dgrbh2[indx]+ - gquinc[1]*(Dgrbh2[indx-m1]+Dgrbh2[indx+p1]+Dgrbh2[indx-p1]+Dgrbh2[indx+m1])+ - gquinc[2]*(Dgrbh2[indx-v2]+Dgrbh2[indx-2]+Dgrbh2[indx+2]+Dgrbh2[indx+v2])+ - gquinc[3]*(Dgrbh2[indx-m2]+Dgrbh2[indx+p2]+Dgrbh2[indx-p2]+Dgrbh2[indx+m2])); - gvarv = epssq + (gquinc[0]*Dgrbv2[indx]+ - gquinc[1]*(Dgrbv2[indx-m1]+Dgrbv2[indx+p1]+Dgrbv2[indx-p1]+Dgrbv2[indx+m1])+ - gquinc[2]*(Dgrbv2[indx-v2]+Dgrbv2[indx-2]+Dgrbv2[indx+2]+Dgrbv2[indx+v2])+ - gquinc[3]*(Dgrbv2[indx-m2]+Dgrbv2[indx+p2]+Dgrbv2[indx-p2]+Dgrbv2[indx+m2])); + gvarh = epssq + (gquinc[0]*Dgrb2[indx>>1].h+ + gquinc[1]*(Dgrb2[(indx-m1)>>1].h+Dgrb2[(indx+p1)>>1].h+Dgrb2[(indx-p1)>>1].h+Dgrb2[(indx+m1)>>1].h)+ + gquinc[2]*(Dgrb2[(indx-v2)>>1].h+Dgrb2[(indx-2)>>1].h+Dgrb2[(indx+2)>>1].h+Dgrb2[(indx+v2)>>1].h)+ + gquinc[3]*(Dgrb2[(indx-m2)>>1].h+Dgrb2[(indx+p2)>>1].h+Dgrb2[(indx-p2)>>1].h+Dgrb2[(indx+m2)>>1].h)); + gvarv = epssq + (gquinc[0]*Dgrb2[indx>>1].v+ + gquinc[1]*(Dgrb2[(indx-m1)>>1].v+Dgrb2[(indx+p1)>>1].v+Dgrb2[(indx-p1)>>1].v+Dgrb2[(indx+m1)>>1].v)+ + gquinc[2]*(Dgrb2[(indx-v2)>>1].v+Dgrb2[(indx-2)>>1].v+Dgrb2[(indx+2)>>1].v+Dgrb2[(indx+v2)>>1].v)+ + gquinc[3]*(Dgrb2[(indx-m2)>>1].v+Dgrb2[(indx+p2)>>1].v+Dgrb2[(indx-p2)>>1].v+Dgrb2[(indx+m2)>>1].v)); //use the results as weights for refined G interpolation Dgrb[indx][0] = (hcd[indx]*gvarv + vcd[indx]*gvarh)/(gvarv+gvarh); rgb[indx][1] = cfa[indx] + Dgrb[indx][0]; @@ -754,66 +807,75 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { // diagonal interpolation correction for (rr=8; rr>1; cc>1]+delm[(indx+m2)>>1];//same as for wtu,wtd,wtl,wtr + wtnw= eps+delm[indx1]+delm[(indx-m1)>>1]+delm[(indx-m2)>>1]; + wtne= eps+delp[indx1]+delp[(indx+p1)>>1]+delp[(indx+p2)>>1]; + wtsw= eps+delp[indx1]+delp[(indx-p1)>>1]+delp[(indx-p2)>>1]; - rbm[indx] = (wtse*rbnw+wtnw*rbse)/(wtse+wtnw); - rbp[indx] = (wtne*rbsw+wtsw*rbne)/(wtne+wtsw); - - pmwt[indx] = rbvarm/(rbvarp+rbvarm); + rb[indx1].m = (wtse*rbnw+wtnw*rbse)/(wtse+wtnw); + rb[indx1].p = (wtne*rbsw+wtsw*rbne)/(wtne+wtsw); +/* + rbvarp = epssq + (gausseven[0]*(Dgrbsq1[indx-v1].p+Dgrbsq1[indx-1].p+Dgrbsq1[indx+1].p+Dgrbsq1[indx+v1].p) + + gausseven[1]*(Dgrbsq1[indx-v2-1].p+Dgrbsq1[indx-v2+1].p+Dgrbsq1[indx-2-v1].p+Dgrbsq1[indx+2-v1].p+ + Dgrbsq1[indx-2+v1].p+Dgrbsq1[indx+2+v1].p+Dgrbsq1[indx+v2-1].p+Dgrbsq1[indx+v2+1].p)); +*/ + rbvarm = epssq + (gausseven[0]*(Dgrbsq1[(indx-v1)>>1].m+Dgrbsq1[(indx-1)>>1].m+Dgrbsq1[(indx+1)>>1].m+Dgrbsq1[(indx+v1)>>1].m) + + gausseven[1]*(Dgrbsq1[(indx-v2-1)>>1].m+Dgrbsq1[(indx-v2+1)>>1].m+Dgrbsq1[(indx-2-v1)>>1].m+Dgrbsq1[(indx+2-v1)>>1].m+ + Dgrbsq1[(indx-2+v1)>>1].m+Dgrbsq1[(indx+2+v1)>>1].m+Dgrbsq1[(indx+v2-1)>>1].m+Dgrbsq1[(indx+v2+1)>>1].m)); + pmwt[indx1] = rbvarm/((epssq + (gausseven[0]*(Dgrbsq1[(indx-v1)>>1].p+Dgrbsq1[(indx-1)>>1].p+Dgrbsq1[(indx+1)>>1].p+Dgrbsq1[(indx+v1)>>1].p) + + gausseven[1]*(Dgrbsq1[(indx-v2-1)>>1].p+Dgrbsq1[(indx-v2+1)>>1].p+Dgrbsq1[(indx-2-v1)>>1].p+Dgrbsq1[(indx+2-v1)>>1].p+ + Dgrbsq1[(indx-2+v1)>>1].p+Dgrbsq1[(indx+2+v1)>>1].p+Dgrbsq1[(indx+v2-1)>>1].p+Dgrbsq1[(indx+v2+1)>>1].p)))+rbvarm); // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //bound the interpolation in regions of high saturation - if (rbp[indx] clip_pt) rbp[indx]=ULIM(rbp[indx],cfa[indx-p1],cfa[indx+p1]);//for RT implementation - if (rbm[indx] > clip_pt) rbm[indx]=ULIM(rbm[indx],cfa[indx-m1],cfa[indx+m1]); + if (rb[indx1].p > clip_pt) rb[indx1].p=ULIM(rb[indx1].p,cfa[indx-p1],cfa[indx+p1]);//for RT implementation + if (rb[indx1].m > clip_pt) rb[indx1].m=ULIM(rb[indx1].m,cfa[indx-m1],cfa[indx+m1]); //c=2-FC(rr,cc);//for dcraw implementation //if (rbp[indx] > pre_mul[c]) rbp[indx]=ULIM(rbp[indx],cfa[indx-p1],cfa[indx+p1]); //if (rbm[indx] > pre_mul[c]) rbm[indx]=ULIM(rbm[indx],cfa[indx-m1],cfa[indx+m1]); @@ -825,39 +887,41 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { for (rr=10; rr>1; cc>1]+pmwt[(indx+p1)>>1]+pmwt[(indx-p1)>>1]+pmwt[(indx+m1)>>1],2); +// vo=fabsf(0.5-pmwt[indx1]); +// ve=fabsf(0.5-pmwtalt); + if (fabsf(0.5-pmwt[indx1])>1; cc>1])>1]) ) continue; //now interpolate G vertically/horizontally using R+B values //unfortunately, since G interpolation cannot be done diagonally this may lead to color shifts //color ratios for G interpolation - cru = cfa[indx-v1]*2.0/(eps+rbint[indx]+rbint[indx-v2]); - crd = cfa[indx+v1]*2.0/(eps+rbint[indx]+rbint[indx+v2]); - crl = cfa[indx-1]*2.0/(eps+rbint[indx]+rbint[indx-2]); - crr = cfa[indx+1]*2.0/(eps+rbint[indx]+rbint[indx+2]); + cru = cfa[indx-v1]*2.0/(eps+rbint[indx1]+rbint[(indx1-v1)]); + crd = cfa[indx+v1]*2.0/(eps+rbint[indx1]+rbint[(indx1+v1)]); + crl = cfa[indx-1]*2.0/(eps+rbint[indx1]+rbint[(indx1-1)]); + crr = cfa[indx+1]*2.0/(eps+rbint[indx1]+rbint[(indx1+1)]); //interpolated G via adaptive ratios or Hamilton-Adams in each cardinal direction - if (fabs(1.0f-cru) pre_mul[c]) Gintv=ULIM(Gintv,cfa[indx-v1],cfa[indx+v1]); // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - rgb[indx][1] = Ginth*(1.0f-hvwt[indx]) + Gintv*hvwt[indx]; + rgb[indx][1] = Ginth*(1.0f-hvwt[indx1]) + Gintv*hvwt[indx1]; //rgb[indx][1] = 0.5*(rgb[indx][1]+0.25*(rgb[indx-v1][1]+rgb[indx+v1][1]+rgb[indx-1][1]+rgb[indx+1][1])); Dgrb[indx][0] = rgb[indx][1]-cfa[indx]; @@ -915,10 +979,10 @@ void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, int winh) { } for (rr=12; rr>1])*Dgrb[indx-v1][c]+(1.0f-hvwt[(indx+1)>>1])*Dgrb[indx+1][c]+(1.0f-hvwt[(indx-1)>>1])*Dgrb[indx-1][c]+(hvwt[(indx+v1)>>1])*Dgrb[indx+v1][c])/ + ((hvwt[(indx-v1)>>1])+(1.0f-hvwt[(indx+1)>>1])+(1.0f-hvwt[(indx-1)>>1])+(hvwt[(indx+v1)>>1])); } for(rr=12; rr