Bugfix for saturation limiter; bugfix for build using XCode; improvements to Green Equilibration.

This commit is contained in:
Emil Martinec
2011-02-13 14:57:05 -06:00
parent 550a868531
commit c43afe0dfd
13 changed files with 169 additions and 229 deletions

View File

@@ -1,6 +1,27 @@
// CFA pixel cleaning via directional average
// © Emil Martinec
// 2/18/2010
////////////////////////////////////////////////////////////////
//
// Green Equilibration via directional average
//
// copyright (c) 2008-2010 Emil Martinec <ejmartin@uchicago.edu>
//
//
// code dated: February 12, 2011
//
// green_equil_RT.cc is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////
#define TS 256 // Tile size
#include <math.h>
@@ -20,7 +41,6 @@ void RawImageSource::green_equilibrate(float thresh)
static const int v1=TS, v2=2*TS, v3=3*TS, /*v4=4*TS,*/ p1=-TS+1, p2=-2*TS+2, p3=-3*TS+3, m1=TS+1, m2=2*TS+2, m3=3*TS+3;
int height=H, width=W; //for RT only
int top, left;
int verbose=1;
@@ -68,16 +88,12 @@ void RawImageSource::green_equilibrate(float thresh)
int numcols = right - left;
int row, col;
int rr, cc, c, indx;
int rr, cc, indx;
int vote1, vote2;
float val1;
float gin, gse, gsw, gne, gnw, wtse, wtsw, wtne, wtnw;
float gu, gd, gl, gr;
float mcorr, pcorr;
float ginterp;
float diffvarh, diffvarv, hvwt;
// rgb from input CFA data
/* rgb values should be floating point number between 0 and 1
@@ -92,47 +108,58 @@ void RawImageSource::green_equilibrate(float thresh)
//The green equilibration algorithm starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%
// To the extent possible under law, Manuel Llorens <manuelllorens@gmail.com>
// has waived all copyright and related or neighboring rights to this work.
// This code is licensed under CC0 v1.0, see license information at
// http://creativecommons.org/publicdomain/zero/1.0/
double d1,d2,c1,c2;
int o1_1,o1_2,o1_3,o1_4;
int o2_1,o2_2,o2_3,o2_4;
//%%%%%%%%%%%%%%%%%%%%%%
for (rr=2; rr < numrows-2; rr++)
//for (cc=3-(FC(rr,2)&1), indx=rr*TS+cc; cc < numcols-2; cc+=2, indx+=2) {
for (indx=rr*TS+2; indx < rr*TS+numcols-2; indx++) {
for (cc=3-(FC(rr,2)&1), indx=rr*TS+cc; cc < numcols-2; cc+=2, indx+=2) {
if (FC(rr,indx)&1) {
pcorr = (cfa[indx+p1]-cfa[indx])*(cfa[indx-p1]-cfa[indx]);
mcorr = (cfa[indx+m1]-cfa[indx])*(cfa[indx-m1]-cfa[indx]);
if (pcorr>0 && mcorr>0) {checker[indx]=1;} else {checker[indx]=0;}
//checker[indx]=1;//test what happens if we always interpolate
} else {
gu=cfa[indx-v1]+0.5*(cfa[indx]-cfa[indx-v2]);
gd=cfa[indx+v1]+0.5*(cfa[indx]-cfa[indx+v2]);
gl=cfa[indx-1]+0.5*(cfa[indx]-cfa[indx-2]);
gr=cfa[indx+1]+0.5*(cfa[indx]-cfa[indx+2]);
gdiffh[indx] = SQR((gl-gr)/(eps+gl+gr));
gdiffv[indx] = SQR((gu-gd)/(eps+gu+gd));
//gvar[indx] = 0.25*(gu*gu+gd*gd+gl*gl+gr*gr)-SQR(0.25*(gu+gd+gl+gr));
}
pcorr = (cfa[indx+p1]-cfa[indx])*(cfa[indx-p1]-cfa[indx]);
mcorr = (cfa[indx+m1]-cfa[indx])*(cfa[indx-m1]-cfa[indx]);
if (pcorr>0 && mcorr>0) {checker[indx]=1;} else {checker[indx]=0;}
//checker[indx]=1;//test what happens if we always interpolate
}
//now smooth the cfa data
for (rr=6; rr < numrows-6; rr++)
for (rr=6; rr < numrows-6; rr+=2)
for (cc=7-(FC(rr,2)&1), indx=rr*TS+cc; cc < numcols-6; cc+=2, indx+=2) {
if (checker[indx]) {
//%%%%%%%%%%%%%%%%%%%%%%
//neighbor checking code from Manuel Llorens Garcia
o1_1=cfa[(rr-1)*TS+cc-1];
o1_2=cfa[(rr-1)*TS+cc+1];
o1_3=cfa[(rr+1)*TS+cc-1];
o1_4=cfa[(rr+1)*TS+cc+1];
o2_1=cfa[(rr-2)*TS+cc];
o2_2=cfa[(rr+2)*TS+cc];
o2_3=cfa[(rr)*TS+cc-2];
o2_4=cfa[(rr)*TS+cc+2];
diffvarh = eps+(gdiffh[indx-v1]+gdiffh[indx-1]+gdiffh[indx+1]+gdiffh[indx+v1]);
diffvarv = eps+(gdiffv[indx-v1]+gdiffv[indx-1]+gdiffv[indx+1]+gdiffv[indx+v1]);
hvwt = fabs(diffvarv-diffvarh)/(diffvarv+diffvarh);
d1=(o1_1+o1_2+o1_3+o1_4)/4.0;
d2=(o2_1+o2_2+o2_3+o2_4)/4.0;
c1=(fabs(o1_1-o1_2)+fabs(o1_1-o1_3)+fabs(o1_1-o1_4)+fabs(o1_2-o1_3)+fabs(o1_3-o1_4)+fabs(o1_2-o1_4))/6.0;
c2=(fabs(o2_1-o2_2)+fabs(o2_1-o2_3)+fabs(o2_1-o2_4)+fabs(o2_2-o2_3)+fabs(o2_3-o2_4)+fabs(o2_2-o2_4))/6.0;
//%%%%%%%%%%%%%%%%%%%%%%
vote1=(checker[indx-v2]+checker[indx-2]+checker[indx+2]+checker[indx+v2]);
vote2=(checker[indx-m1]+checker[indx+p1]+checker[indx-p1]+checker[indx+m1]);
if (vote1>0 && vote2>0 && hvwt<diffthresh) {
if (vote1>0 && vote2>0 && (c1+c2)<4*thresh*fabs(d1-d2)) {
//pixel interpolation
gin=cfa[indx];
gse=(cfa[indx+m1])+0.5*(cfa[indx]-cfa[indx+m2]);
@@ -149,10 +176,7 @@ void RawImageSource::green_equilibrate(float thresh)
ginterp=(gse*wtse+gnw*wtnw+gne*wtne+gsw*wtsw)/(wtse+wtnw+wtne+wtsw);
if (/*(SQR(ginterp-gin) > 0.125*(gvar[indx-1]+gvar[indx+1]+gvar[indx-v1]+gvar[indx+v1])) &&*/ ((ginterp-gin) < thresh*(ginterp+gin)) ) {
cfa[indx]=0.5*(ginterp+gin);
}
cfa[indx]=ginterp;//0.5*(ginterp+gin);
}
}
}
@@ -160,15 +184,14 @@ void RawImageSource::green_equilibrate(float thresh)
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// copy smoothed results back to image matrix
for (rr=border; rr < numrows-border; rr++)
for (rr=border; rr < numrows-border; rr+=2)
for (row=rr+top, cc=border+1-(FC(rr,2)&1), indx=rr*TS+cc; cc < numcols-border; cc+=2, indx+=2) {
if (cfa[indx]<1) continue;
col = cc + left;
//c = FC(row,col);
//image[row*width + col][c] = CLIP((int)(cfa[indx] + 0.5)); //for dcraw implementation
rawData[row][col] = CLIP((int)(cfa[indx] + 0.5));
}
// clean up
}
free(buffer);