From 94129861f5366a620de1ab3d99855656793f23e5 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Sun, 19 Feb 2017 20:36:45 +0100 Subject: [PATCH 1/6] Speedup for lcp vignetting correction --- rtengine/lcp.cc | 66 ++++++++++++++++++++++++++------------ rtengine/lcp.h | 15 ++++++--- rtengine/rawimagesource.cc | 24 +++++++++++--- 3 files changed, 77 insertions(+), 28 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index ce1a6c67e..8579efcbd 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -19,32 +19,28 @@ #include #include "lcp.h" -#include "iccmatrices.h" -#include "iccstore.h" -#include "rawimagesource.h" -#include "improcfun.h" -#include "rt_math.h" +#include #ifdef WIN32 #include -// for GCC32 -#ifndef _WIN32_IE -#define _WIN32_IE 0x0600 -#endif #include #endif using namespace std; using namespace rtengine; -using namespace rtexif; LCPModelCommon::LCPModelCommon() { focLenX = focLenY = -1; imgXCenter = imgYCenter = 0.5; - x0 = y0 = fx = fy = meanErr = 0; + x0 = y0 = fx = fy = rfx = rfy = meanErr = 0; + +#if defined( __SSE2__ ) && defined( __x86_64__ ) + x0v = y0v = rfxv = rfyv = ZEROV; +#endif + badErr = false; for (int i = 0; i < 5; i++) { @@ -66,7 +62,7 @@ void LCPModelCommon::print() const printf("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); } -// weightened merge two parameters +// weighted merge two parameters void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA) { float facB = 1 - facA; @@ -81,6 +77,20 @@ void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, flo for (int i = 0; i < 5; i++) { param[i] = facA * a.param[i] + facB * b.param[i]; } + + double param0Sqr = param[0] * param[0]; + + vignParam[0] = - param[0]; + vignParam[1] = param0Sqr - param[1]; + vignParam[2] = param0Sqr * param[0] - 2. * param[0] * param[1] + param[2]; + vignParam[3] = param0Sqr * param0Sqr + param[1] * param[1] + 2. * param[0] * param[2] - 3. * param0Sqr * param[1]; + +#if defined( __SSE2__ ) && defined( __x86_64__ ) + vignParamv[0] = F2V(vignParam[0]); + vignParamv[1] = F2V(vignParam[1]); + vignParamv[2] = F2V(vignParam[2]); + vignParamv[3] = F2V(vignParam[3]); +#endif // __SSE2__ } void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) @@ -113,6 +123,15 @@ void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLen fx = focLenX * Dmax; fy = focLenY * Dmax; } + rfx = 1.0 / fx; + rfy = 1.0 / fy; + +#if defined( __SSE2__ ) && defined( __x86_64__ ) + x0v = F2V(x0); + y0v = F2V(y0); + rfxv = F2V(rfx); + rfyv = F2V(rfy); +#endif //printf("FW %i /X0 %g FH %i /Y0 %g %g\n",fullWidth,x0,fullHeight,y0, imgYCenter); } @@ -264,18 +283,25 @@ void LCPMapper::correctCA(double& x, double& y, int channel) const float LCPMapper::calcVignetteFac(int x, int y) const { // No need for swapXY, since vignette is in RAW and always before rotation - double xd = ((double)x - mc.x0) / mc.fx, yd = ((double)y - mc.y0) / mc.fy; + float xd = ((float)x - mc.x0) * mc.rfx, yd = ((float)y - mc.y0) * mc.rfy; - const float* aVig = mc.param; - double rsqr = xd * xd + yd * yd; - double param0Sqr = aVig[0] * aVig[0]; + const float* vignParam = mc.vignParam; + float rsqr = xd * xd + yd * yd; - return 1. + rsqr * (-aVig[0] + rsqr * ((param0Sqr - aVig[1]) - - (param0Sqr * aVig[0] - 2.*aVig[0] * aVig[1] + aVig[2]) * rsqr - + (param0Sqr * param0Sqr + aVig[1] * aVig[1] - + 2.*aVig[0] * aVig[2] - 3.*param0Sqr * aVig[1]) * rsqr * rsqr)); + return 1.f + rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); } +#if defined( __SSE2__ ) && defined( __x86_64__ ) +vfloat LCPMapper::calcVignetteFac(vfloat x, vfloat y) const +{ + // No need for swapXY, since vignette is in RAW and always before rotation + vfloat xd = (x - mc.x0v) * mc.rfxv, yd = (y - mc.y0v) * mc.rfyv; + vfloat rsqr = xd * xd + yd * yd; + + return F2V(1.f) + rsqr * (mc.vignParamv[0] + rsqr * (mc.vignParamv[1] - mc.vignParamv[2] * rsqr + mc.vignParamv[3] * rsqr * rsqr)); +} +#endif + LCPProfile::LCPProfile(const Glib::ustring &fname) { const int BufferSize = 8192; diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 908bacf56..74d06c958 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -21,10 +21,9 @@ #define _LCP_ #include "imagefloat.h" -#include "../rtgui/threadutils.h" +#include "opthelper.h" #include #include -#include #include #include @@ -40,8 +39,13 @@ public: double meanErr; bool badErr; - double x0, y0, fx, fy; // prepared params - + float x0, y0, fx, fy; // prepared params + float rfx, rfy; + float vignParam[4]; +#if defined( __SSE2__ ) && defined( __x86_64__ ) + vfloat vignParamv[4] ALIGNED16; + vfloat x0v, y0v, rfxv, rfyv; +#endif LCPModelCommon(); bool empty() const; // is it empty void print() const; // printf all values @@ -133,6 +137,9 @@ public: void correctDistortion(double& x, double& y) const; // MUST be the first stage void correctCA(double& x, double& y, int channel) const; float calcVignetteFac (int x, int y) const; // MUST be in RAW +#if defined( __SSE2__ ) && defined( __x86_64__ ) + vfloat calcVignetteFac(vfloat x, vfloat y) const; +#endif }; } #endif diff --git a/rtengine/rawimagesource.cc b/rtengine/rawimagesource.cc index 9c8f9788b..47613cf1a 100644 --- a/rtengine/rawimagesource.cc +++ b/rtengine/rawimagesource.cc @@ -1767,15 +1767,31 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le LCPProfile *pLCPProf = lcpStore->getProfile(lensProf.lcpFile); if (pLCPProf) { // don't check focal length to allow distortion correction for lenses without chip, also pass dummy focal length 1 in case of 0 + StopWatch Stop1("lcp vignette correction"); LCPMapper map(pLCPProf, max(idata->getFocalLen(), 1.0), idata->getFocalLen35mm(), idata->getFocusDist(), idata->getFNumber(), true, false, W, H, coarse, -1); -if (ri->getSensorType() == ST_BAYER || ri->getSensorType() == ST_FUJI_XTRANS || ri->get_colors() == 1) { + if (ri->getSensorType() == ST_BAYER || ri->getSensorType() == ST_FUJI_XTRANS || ri->get_colors() == 1) { #ifdef _OPENMP - #pragma omp parallel for + #pragma omp parallel for schedule(dynamic,16) #endif for (int y = 0; y < H; y++) { - for (int x = 0; x < W; x++) { + int x = 0; + +#if defined( __SSE2__ ) && defined( __x86_64__ ) + vfloat yv = F2V(y); + vfloat fourv = F2V(4.f); + vfloat onev = F2V(1.f); + vfloat xv = _mm_set_ps(3,2,1,0); + for (; x < W-3; x+=4) { + vfloat vignFactorv = map.calcVignetteFac(xv, yv); + vfloat rawValv = LVFU(rawData[y][x]); + rawValv *= vself(vmaskf_gt(rawValv, ZEROV), vignFactorv, onev); + STVFU(rawData[y][x], rawValv); + xv += fourv; + } +#endif + for (; x < W; x++) { if (rawData[y][x] > 0) { rawData[y][x] *= map.calcVignetteFac(x, y); } @@ -1783,7 +1799,7 @@ if (ri->getSensorType() == ST_BAYER || ri->getSensorType() == ST_FUJI_XTRANS || } } else if(ri->get_colors() == 3) { #ifdef _OPENMP - #pragma omp parallel for + #pragma omp parallel for schedule(dynamic,16) #endif for (int y = 0; y < H; y++) { From 87a280f8cafeb8aeaab81292b12a4b7fec61dcc3 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Tue, 21 Feb 2017 19:11:54 +0100 Subject: [PATCH 2/6] Additional speedup for lcp vignette correction --- rtengine/lcp.cc | 77 ++++++++++++++++++++++++++------------ rtengine/lcp.h | 9 +---- rtengine/rawimagesource.cc | 31 ++------------- 3 files changed, 58 insertions(+), 59 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index 8579efcbd..5dd51b58f 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -37,10 +37,6 @@ LCPModelCommon::LCPModelCommon() imgXCenter = imgYCenter = 0.5; x0 = y0 = fx = fy = rfx = rfy = meanErr = 0; -#if defined( __SSE2__ ) && defined( __x86_64__ ) - x0v = y0v = rfxv = rfyv = ZEROV; -#endif - badErr = false; for (int i = 0; i < 5; i++) { @@ -85,12 +81,6 @@ void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, flo vignParam[2] = param0Sqr * param[0] - 2. * param[0] * param[1] + param[2]; vignParam[3] = param0Sqr * param0Sqr + param[1] * param[1] + 2. * param[0] * param[2] - 3. * param0Sqr * param[1]; -#if defined( __SSE2__ ) && defined( __x86_64__ ) - vignParamv[0] = F2V(vignParam[0]); - vignParamv[1] = F2V(vignParam[1]); - vignParamv[2] = F2V(vignParam[2]); - vignParamv[3] = F2V(vignParam[3]); -#endif // __SSE2__ } void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) @@ -126,13 +116,6 @@ void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLen rfx = 1.0 / fx; rfy = 1.0 / fy; -#if defined( __SSE2__ ) && defined( __x86_64__ ) - x0v = F2V(x0); - y0v = F2V(y0); - rfxv = F2V(rfx); - rfyv = F2V(rfy); -#endif - //printf("FW %i /X0 %g FH %i /Y0 %g %g\n",fullWidth,x0,fullHeight,y0, imgYCenter); } @@ -288,19 +271,65 @@ float LCPMapper::calcVignetteFac(int x, int y) const const float* vignParam = mc.vignParam; float rsqr = xd * xd + yd * yd; - return 1.f + rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); + return rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); } -#if defined( __SSE2__ ) && defined( __x86_64__ ) -vfloat LCPMapper::calcVignetteFac(vfloat x, vfloat y) const +SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation - vfloat xd = (x - mc.x0v) * mc.rfxv, yd = (y - mc.y0v) * mc.rfyv; - vfloat rsqr = xd * xd + yd * yd; + float yd = ((float)y - mc.y0) * mc.rfy; + yd *= yd; + int x = 0; +#ifdef __SSE2__ + const vfloat fourv = F2V(4.f); + const vfloat zerov = F2V(0.f); + const vfloat ydv = F2V(yd); + const vfloat p0 = F2V(mc.vignParam[0]); + const vfloat p1 = F2V(mc.vignParam[1]); + const vfloat p2 = F2V(mc.vignParam[2]); + const vfloat p3 = F2V(mc.vignParam[3]); + const vfloat x0v = F2V(mc.x0); + const vfloat rfxv = F2V(mc.rfx); - return F2V(1.f) + rsqr * (mc.vignParamv[0] + rsqr * (mc.vignParamv[1] - mc.vignParamv[2] * rsqr + mc.vignParamv[3] * rsqr * rsqr)); + vfloat xv = _mm_setr_ps(0.f, 1.f, 2.f, 3.f); + for (; x < width-3; x+=4) { + vfloat xdv = (xv - x0v) * rfxv; + vfloat rsqr = xdv * xdv + ydv; + vfloat vignFactorv = rsqr * (p0 + rsqr * (p1 - p2 * rsqr + p3 * rsqr * rsqr)); + vfloat valv = LVFU(line[x]); + valv += valv * vselfzero(vmaskf_gt(valv, zerov), vignFactorv); + STVFU(line[x], valv); + xv += fourv; + } +#endif // __SSE2__ + for (; x < width; x++) { + if (line[x] > 0) { + float xd = ((float)x - mc.x0) * mc.rfx; + const float* vignParam = mc.vignParam; + float rsqr = xd * xd + yd; + line[x] += line[x] * rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); + } + } } -#endif + +SSEFUNCTION void LCPMapper::processVignetteLine3Channels(int width, int y, float *line) const +{ + // No need for swapXY, since vignette is in RAW and always before rotation + float yd = ((float)y - mc.y0) * mc.rfy; + yd *= yd; + const float* vignParam = mc.vignParam; + for (int x = 0; x < width; x++) { + float xd = ((float)x - mc.x0) * mc.rfx; + float rsqr = xd * xd + yd; + float vignetteFactor = rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); + for(int c = 0;c < 3; ++c) { + if (line[3*x+c] > 0) { + line[3*x+c] += line[3*x+c] * vignetteFactor; + } + } + } +} + LCPProfile::LCPProfile(const Glib::ustring &fname) { diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 74d06c958..449add15c 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -42,10 +42,6 @@ public: float x0, y0, fx, fy; // prepared params float rfx, rfy; float vignParam[4]; -#if defined( __SSE2__ ) && defined( __x86_64__ ) - vfloat vignParamv[4] ALIGNED16; - vfloat x0v, y0v, rfxv, rfyv; -#endif LCPModelCommon(); bool empty() const; // is it empty void print() const; // printf all values @@ -137,9 +133,8 @@ public: void correctDistortion(double& x, double& y) const; // MUST be the first stage void correctCA(double& x, double& y, int channel) const; float calcVignetteFac (int x, int y) const; // MUST be in RAW -#if defined( __SSE2__ ) && defined( __x86_64__ ) - vfloat calcVignetteFac(vfloat x, vfloat y) const; -#endif + void processVignetteLine(int width, int y, float *line) const; + void processVignetteLine3Channels(int width, int y, float *line) const; }; } #endif diff --git a/rtengine/rawimagesource.cc b/rtengine/rawimagesource.cc index 47613cf1a..995e3d9bd 100644 --- a/rtengine/rawimagesource.cc +++ b/rtengine/rawimagesource.cc @@ -1771,31 +1771,13 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le LCPMapper map(pLCPProf, max(idata->getFocalLen(), 1.0), idata->getFocalLen35mm(), idata->getFocusDist(), idata->getFNumber(), true, false, W, H, coarse, -1); if (ri->getSensorType() == ST_BAYER || ri->getSensorType() == ST_FUJI_XTRANS || ri->get_colors() == 1) { + #ifdef _OPENMP #pragma omp parallel for schedule(dynamic,16) #endif for (int y = 0; y < H; y++) { - int x = 0; - -#if defined( __SSE2__ ) && defined( __x86_64__ ) - vfloat yv = F2V(y); - vfloat fourv = F2V(4.f); - vfloat onev = F2V(1.f); - vfloat xv = _mm_set_ps(3,2,1,0); - for (; x < W-3; x+=4) { - vfloat vignFactorv = map.calcVignetteFac(xv, yv); - vfloat rawValv = LVFU(rawData[y][x]); - rawValv *= vself(vmaskf_gt(rawValv, ZEROV), vignFactorv, onev); - STVFU(rawData[y][x], rawValv); - xv += fourv; - } -#endif - for (; x < W; x++) { - if (rawData[y][x] > 0) { - rawData[y][x] *= map.calcVignetteFac(x, y); - } - } + map.processVignetteLine(W, y, rawData[y]); } } else if(ri->get_colors() == 3) { #ifdef _OPENMP @@ -1803,14 +1785,7 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le #endif for (int y = 0; y < H; y++) { - for (int x = 0; x < W; x++) { - float vignFactor = map.calcVignetteFac(x, y); - for(int c = 0;c < 3; ++c) { - if (rawData[y][3 * x + c] > 0) { - rawData[y][3 * x + c] *= vignFactor; - } - } - } + map.processVignetteLine3Channels(W, y, rawData[y]); } } } From 99309aa4ace072af51ce74614153c805ad13db9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Tue, 21 Feb 2017 21:10:33 +0100 Subject: [PATCH 3/6] Preliminary cleanup for `LCPModelCommon` --- rtengine/lcp.cc | 166 ++++++++++++++++++++++++------------------------ rtengine/lcp.h | 50 ++++++++++----- 2 files changed, 117 insertions(+), 99 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index 5dd51b58f..ff53ad30e 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -16,6 +16,8 @@ * You should have received a copy of the GNU General Public License * along with RawTherapee. If not, see . */ + +#include #include #include "lcp.h" @@ -31,29 +33,33 @@ using namespace std; using namespace rtengine; -LCPModelCommon::LCPModelCommon() +LCPModelCommon::LCPModelCommon() : + foc_len_x(-1.0f), + foc_len_y(-1.0f), + img_center_x(0.5f), + img_center_y(0.5f), + param{{}}, + scale_factor(1.0f), + mean_error(0.0), + bad_error(false), + x0(0.0f), + y0(0.0f), + fx(0.0f), + fy(0.0f), + rfx(0.0f), + rfy(0.0f), + vign_param{{}} { - focLenX = focLenY = -1; - imgXCenter = imgYCenter = 0.5; - x0 = y0 = fx = fy = rfx = rfy = meanErr = 0; - - badErr = false; - - for (int i = 0; i < 5; i++) { - param[i] = 0; - } - - scaleFac = 1; } bool LCPModelCommon::empty() const { - return param[0] == 0 && param[1] == 0 && param[2] == 0; + return param[0] == 0.0f && param[1] == 0.0f && param[2] == 0.0f; } void LCPModelCommon::print() const { - printf("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", focLenX, focLenY, imgXCenter, imgYCenter, scaleFac, meanErr); + printf("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", foc_len_x, foc_len_y, img_center_x, img_center_y, scale_factor, mean_error); printf("xy0 %g/%g fxy %g/%g\n", x0, y0, fx, fy); printf("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); } @@ -61,60 +67,56 @@ void LCPModelCommon::print() const // weighted merge two parameters void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA) { - float facB = 1 - facA; + const float facB = 1.0f - facA; - focLenX = facA * a.focLenX + facB * b.focLenX; - focLenY = facA * a.focLenY + facB * b.focLenY; - imgXCenter = facA * a.imgXCenter + facB * b.imgXCenter; - imgYCenter = facA * a.imgYCenter + facB * b.imgYCenter; - scaleFac = facA * a.scaleFac + facB * b.scaleFac; - meanErr = facA * a.meanErr + facB * b.meanErr; + foc_len_x = facA * a.foc_len_x + facB * b.foc_len_x; + foc_len_y = facA * a.foc_len_y + facB * b.foc_len_y; + img_center_x = facA * a.img_center_x + facB * b.img_center_x; + img_center_y = facA * a.img_center_y + facB * b.img_center_y; + scale_factor = facA * a.scale_factor + facB * b.scale_factor; + mean_error = facA * a.mean_error + facB * b.mean_error; for (int i = 0; i < 5; i++) { param[i] = facA * a.param[i] + facB * b.param[i]; } - double param0Sqr = param[0] * param[0]; + const float param0Sqr = param[0] * param[0]; - vignParam[0] = - param[0]; - vignParam[1] = param0Sqr - param[1]; - vignParam[2] = param0Sqr * param[0] - 2. * param[0] * param[1] + param[2]; - vignParam[3] = param0Sqr * param0Sqr + param[1] * param[1] + 2. * param[0] * param[2] - 3. * param0Sqr * param[1]; + vign_param[0] = -param[0]; + vign_param[1] = param0Sqr - param[1]; + vign_param[2] = param0Sqr * param[0] - 2.0f * param[0] * param[1] + param[2]; + vign_param[3] = param0Sqr * param0Sqr + param[1] * param[1] + 2.0f * param[0] * param[2] - 3.0f * param0Sqr * param[1]; } void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) { // Mention that the Adobe technical paper has a bug here, the DMAX is handled differently for focLen and imgCenter - int Dmax = fullWidth; - - if (fullHeight > fullWidth) { - Dmax = fullHeight; - } + const int Dmax = std::max(fullWidth, fullHeight); // correct focLens - if (focLenX < 0) { // they may not be given + if (foc_len_x < 0.0f) { // they may not be given // and 35mm may not be given either - if (focalLength35mm < 1) { + if (focalLength35mm < 1.0f) { focalLength35mm = focalLength * sensorFormatFactor; } - focLenX = focLenY = focalLength / ( 35 * focalLength / focalLength35mm); // focLen must be calculated in pixels + foc_len_x = foc_len_y = focalLength / (35.0f * focalLength / focalLength35mm); // focLen must be calculated in pixels } if (swapXY) { - x0 = (mirrorX ? 1. - imgYCenter : imgYCenter) * fullWidth; - y0 = (mirrorY ? 1. - imgXCenter : imgXCenter) * fullHeight; - fx = focLenY * Dmax; - fy = focLenX * Dmax; + x0 = (mirrorX ? 1.0f - img_center_y : img_center_y) * fullWidth; + y0 = (mirrorY ? 1.0f - img_center_x : img_center_x) * fullHeight; + fx = foc_len_y * Dmax; + fy = foc_len_x * Dmax; } else { - x0 = (mirrorX ? 1. - imgXCenter : imgXCenter) * fullWidth; - y0 = (mirrorY ? 1. - imgYCenter : imgYCenter) * fullHeight; - fx = focLenX * Dmax; - fy = focLenY * Dmax; + x0 = (mirrorX ? 1.0f - img_center_x : img_center_x) * fullWidth; + y0 = (mirrorY ? 1.0f - img_center_y : img_center_y) * fullHeight; + fx = foc_len_x * Dmax; + fy = foc_len_y * Dmax; } - rfx = 1.0 / fx; - rfy = 1.0 / fy; + rfx = 1.0f / fx; + rfy = 1.0f / fy; //printf("FW %i /X0 %g FH %i /Y0 %g %g\n",fullWidth,x0,fullHeight,y0, imgYCenter); } @@ -127,9 +129,9 @@ LCPPersModel::LCPPersModel() // mode: 0=distortion, 1=vignette, 2=CA bool LCPPersModel::hasModeData(int mode) const { - return (mode == 0 && !vignette.empty() && !vignette.badErr) || (mode == 1 && !base.empty() && !base.badErr) + return (mode == 0 && !vignette.empty() && !vignette.bad_error) || (mode == 1 && !base.empty() && !base.bad_error) || (mode == 2 && !chromRG.empty() && !chromG.empty() && !chromBG.empty() && - !chromRG.badErr && !chromG.badErr && !chromBG.badErr); + !chromRG.bad_error && !chromG.bad_error && !chromBG.bad_error); } void LCPPersModel::print() const @@ -202,7 +204,7 @@ void LCPMapper::correctDistortion(double& x, double& y) const { double xd = (x - mc.x0) / mc.fx, yd = (y - mc.y0) / mc.fy; - const float* aDist = mc.param; + const LCPModelCommon::Param aDist = mc.param; double rsqr = xd * xd + yd * yd; double xfac = aDist[swapXY ? 3 : 4], yfac = aDist[swapXY ? 4 : 3]; @@ -230,7 +232,7 @@ void LCPMapper::correctCA(double& x, double& y, int channel) const // Green contains main distortion, just like base if (useCADist) { - const float* aDist = chrom[1].param; + const LCPModelCommon::Param aDist = chrom[1].param; double rsqr = xd * xd + yd * yd; double xfac = aDist[swapXY ? 3 : 4], yfac = aDist[swapXY ? 4 : 3]; @@ -254,12 +256,12 @@ void LCPMapper::correctCA(double& x, double& y, int channel) const yd = ygreen; rsqr = xd * xd + yd * yd; - const float* aCA = chrom[channel].param; + const LCPModelCommon::Param aCA = chrom[channel].param; double xfac = aCA[swapXY ? 3 : 4], yfac = aCA[swapXY ? 4 : 3]; double commonSum = 1. + rsqr * (aCA[0] + rsqr * (aCA[1] + aCA[2] * rsqr)) + 2. * (yfac * yd + xfac * xd); - x = (chrom[channel].scaleFac * ( xd * commonSum + xfac * rsqr )) * chrom[channel].fx + chrom[channel].x0; - y = (chrom[channel].scaleFac * ( yd * commonSum + yfac * rsqr )) * chrom[channel].fy + chrom[channel].y0; + x = (chrom[channel].scale_factor * ( xd * commonSum + xfac * rsqr )) * chrom[channel].fx + chrom[channel].x0; + y = (chrom[channel].scale_factor * ( yd * commonSum + yfac * rsqr )) * chrom[channel].fy + chrom[channel].y0; } } @@ -268,7 +270,7 @@ float LCPMapper::calcVignetteFac(int x, int y) const // No need for swapXY, since vignette is in RAW and always before rotation float xd = ((float)x - mc.x0) * mc.rfx, yd = ((float)y - mc.y0) * mc.rfy; - const float* vignParam = mc.vignParam; + const LCPModelCommon::VignParam vignParam = mc.vign_param; float rsqr = xd * xd + yd * yd; return rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); @@ -284,10 +286,10 @@ SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) c const vfloat fourv = F2V(4.f); const vfloat zerov = F2V(0.f); const vfloat ydv = F2V(yd); - const vfloat p0 = F2V(mc.vignParam[0]); - const vfloat p1 = F2V(mc.vignParam[1]); - const vfloat p2 = F2V(mc.vignParam[2]); - const vfloat p3 = F2V(mc.vignParam[3]); + const vfloat p0 = F2V(mc.vign_param[0]); + const vfloat p1 = F2V(mc.vign_param[1]); + const vfloat p2 = F2V(mc.vign_param[2]); + const vfloat p3 = F2V(mc.vign_param[3]); const vfloat x0v = F2V(mc.x0); const vfloat rfxv = F2V(mc.rfx); @@ -305,7 +307,7 @@ SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) c for (; x < width; x++) { if (line[x] > 0) { float xd = ((float)x - mc.x0) * mc.rfx; - const float* vignParam = mc.vignParam; + const LCPModelCommon::VignParam vignParam = mc.vign_param; float rsqr = xd * xd + yd; line[x] += line[x] * rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); } @@ -317,7 +319,7 @@ SSEFUNCTION void LCPMapper::processVignetteLine3Channels(int width, int y, float // No need for swapXY, since vignette is in RAW and always before rotation float yd = ((float)y - mc.y0) * mc.rfy; yd *= yd; - const float* vignParam = mc.vignParam; + const LCPModelCommon::VignParam vignParam = mc.vign_param; for (int x = 0; x < width; x++) { float xd = ((float)x - mc.x0) * mc.rfx; float rsqr = xd * xd + yd; @@ -391,17 +393,17 @@ int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { if (aPersModel[pm]->hasModeData(0)) { - errVignette += aPersModel[pm]->vignette.meanErr; + errVignette += aPersModel[pm]->vignette.mean_error; vignetteCount++; } if (aPersModel[pm]->hasModeData(1)) { - errBase += aPersModel[pm]->base.meanErr; + errBase += aPersModel[pm]->base.mean_error; baseCount++; } if (aPersModel[pm]->hasModeData(2)) { - errChrom += std::max(std::max(aPersModel[pm]->chromRG.meanErr, aPersModel[pm]->chromG.meanErr), aPersModel[pm]->chromBG.meanErr); + errChrom += std::max(std::max(aPersModel[pm]->chromRG.mean_error, aPersModel[pm]->chromG.mean_error), aPersModel[pm]->chromBG.mean_error); chromCount++; } } @@ -424,20 +426,20 @@ int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) // Now mark all the bad ones as bad, and hasModeData will return false; for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { - if (aPersModel[pm]->hasModeData(0) && aPersModel[pm]->vignette.meanErr > maxAvgDevFac * errVignette) { - aPersModel[pm]->vignette.badErr = true; + if (aPersModel[pm]->hasModeData(0) && aPersModel[pm]->vignette.mean_error > maxAvgDevFac * errVignette) { + aPersModel[pm]->vignette.bad_error = true; filtered++; } - if (aPersModel[pm]->hasModeData(1) && aPersModel[pm]->base.meanErr > maxAvgDevFac * errBase) { - aPersModel[pm]->base.badErr = true; + if (aPersModel[pm]->hasModeData(1) && aPersModel[pm]->base.mean_error > maxAvgDevFac * errBase) { + aPersModel[pm]->base.bad_error = true; filtered++; } if (aPersModel[pm]->hasModeData(2) && - (aPersModel[pm]->chromRG.meanErr > maxAvgDevFac * errChrom || aPersModel[pm]->chromG.meanErr > maxAvgDevFac * errChrom - || aPersModel[pm]->chromBG.meanErr > maxAvgDevFac * errChrom)) { - aPersModel[pm]->chromRG.badErr = aPersModel[pm]->chromG.badErr = aPersModel[pm]->chromBG.badErr = true; + (aPersModel[pm]->chromRG.mean_error > maxAvgDevFac * errChrom || aPersModel[pm]->chromG.mean_error > maxAvgDevFac * errChrom + || aPersModel[pm]->chromBG.mean_error > maxAvgDevFac * errChrom)) { + aPersModel[pm]->chromRG.bad_error = aPersModel[pm]->chromG.bad_error = aPersModel[pm]->chromBG.bad_error = true; filtered++; } } @@ -493,48 +495,48 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float if (aPersModel[pm]->hasModeData(mode)) { if (mode == 0) { - meanErr = aPersModel[pm]->vignette.meanErr; + meanErr = aPersModel[pm]->vignette.mean_error; // by aperture (vignette), and max out focus distance // tests showed doing this by log(aperture) is not as advisable if (aPersModel[pm]->focLen == bestFocLenLow && ( - (aper == aperture && pLow->vignette.meanErr > meanErr) + (aper == aperture && pLow->vignette.mean_error > meanErr) || (aper >= aperture && aper < pLow->aperture && pLow->aperture > aperture) || (aper <= aperture && (pLow->aperture > aperture || fabs(aperture - aper) < fabs(aperture - pLow->aperture))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( - (aper == aperture && pHigh->vignette.meanErr > meanErr) + (aper == aperture && pHigh->vignette.mean_error > meanErr) || (aper <= aperture && aper > pHigh->aperture && pHigh->aperture < aperture) || (aper >= aperture && (pHigh->aperture < aperture || fabs(aperture - aper) < fabs(aperture - pHigh->aperture))))) { pHigh = aPersModel[pm]; } } else { - meanErr = (mode == 1 ? aPersModel[pm]->base.meanErr : aPersModel[pm]->chromG.meanErr); + meanErr = (mode == 1 ? aPersModel[pm]->base.mean_error : aPersModel[pm]->chromG.mean_error); if (focusDist > 0) { // by focus distance if (aPersModel[pm]->focLen == bestFocLenLow && ( - (focDist == focusDist && (mode == 1 ? pLow->base.meanErr : pLow->chromG.meanErr) > meanErr) + (focDist == focusDist && (mode == 1 ? pLow->base.mean_error : pLow->chromG.mean_error) > meanErr) || (focDist >= focusDist && focDist < pLow->focDist && pLow->focDist > focusDist) || (focDist <= focusDist && (pLow->focDist > focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pLow->focDist) + euler)))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( - (focDist == focusDist && (mode == 1 ? pHigh->base.meanErr : pHigh->chromG.meanErr) > meanErr) + (focDist == focusDist && (mode == 1 ? pHigh->base.mean_error : pHigh->chromG.mean_error) > meanErr) || (focDist <= focusDist && focDist > pHigh->focDist && pHigh->focDist < focusDist) || (focDist >= focusDist && (pHigh->focDist < focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pHigh->focDist) + euler)))))) { pHigh = aPersModel[pm]; } } else { // no focus distance available, just error - if (aPersModel[pm]->focLen == bestFocLenLow && (mode == 1 ? pLow->base.meanErr : pLow->chromG.meanErr) > meanErr) { + if (aPersModel[pm]->focLen == bestFocLenLow && (mode == 1 ? pLow->base.mean_error : pLow->chromG.mean_error) > meanErr) { pLow = aPersModel[pm]; } - if (aPersModel[pm]->focLen == bestFocLenHigh && (mode == 1 ? pHigh->base.meanErr : pHigh->chromG.meanErr) > meanErr) { + if (aPersModel[pm]->focLen == bestFocLenHigh && (mode == 1 ? pHigh->base.mean_error : pHigh->chromG.mean_error) > meanErr) { pHigh = aPersModel[pm]; } } @@ -770,17 +772,17 @@ void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, in // Section depended if (!strcmp("FocalLengthX", tag)) { - pProf->pCurCommon->focLenX = atof(raw); + pProf->pCurCommon->foc_len_x = atof(raw); } else if (!strcmp("FocalLengthY", tag)) { - pProf->pCurCommon->focLenY = atof(raw); + pProf->pCurCommon->foc_len_y = atof(raw); } else if (!strcmp("ImageXCenter", tag)) { - pProf->pCurCommon->imgXCenter = atof(raw); + pProf->pCurCommon->img_center_x = atof(raw); } else if (!strcmp("ImageYCenter", tag)) { - pProf->pCurCommon->imgYCenter = atof(raw); + pProf->pCurCommon->img_center_y = atof(raw); } else if (!strcmp("ScaleFactor", tag)) { - pProf->pCurCommon->scaleFac = atof(raw); + pProf->pCurCommon->scale_factor = atof(raw); } else if (!strcmp("ResidualMeanError", tag)) { - pProf->pCurCommon->meanErr = atof(raw); + pProf->pCurCommon->mean_error = atof(raw); } else if (!strcmp("RadialDistortParam1", tag) || !strcmp("VignetteModelParam1", tag)) { pProf->pCurCommon->param[0] = atof(raw); } else if (!strcmp("RadialDistortParam2", tag) || !strcmp("VignetteModelParam2", tag)) { diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 449add15c..92c984921 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -17,36 +17,52 @@ * along with RawTherapee. If not, see . */ -#ifndef _LCP_ -#define _LCP_ +#pragma once + +#include +#include +#include + +#include +#include #include "imagefloat.h" #include "opthelper.h" -#include -#include -#include -#include namespace rtengine { + // Perspective model common data, also used for Vignette and Fisheye -class LCPModelCommon +class LCPModelCommon final { public: - float focLenX, focLenY, imgXCenter, imgYCenter; - float param[5]; // k1..k5, resp. alpha1..5 - float scaleFac; // alpha0 - double meanErr; - bool badErr; - - float x0, y0, fx, fy; // prepared params - float rfx, rfy; - float vignParam[4]; LCPModelCommon(); bool empty() const; // is it empty void print() const; // printf all values void merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA); void prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY); + +//private: + using Param = std::array; + using VignParam = std::array; + + float foc_len_x; + float foc_len_y; + float img_center_x; + float img_center_y; + Param param; // k1..k5, resp. alpha1..5 + float scale_factor; // alpha0 + double mean_error; + bool bad_error; + + // prepared params + float x0; + float y0; + float fx; + float fy; + float rfx; + float rfy; + VignParam vign_param; }; class LCPPersModel @@ -136,5 +152,5 @@ public: void processVignetteLine(int width, int y, float *line) const; void processVignetteLine3Channels(int width, int y, float *line) const; }; + } -#endif From 1348ea06e4975d26b50f9e723ae61f87916dcf17 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Wed, 22 Feb 2017 01:53:22 +0100 Subject: [PATCH 4/6] Further cleanup and astyled lcp.* --- rtengine/lcp.cc | 401 ++++++++++++++++++++++++------------------------ rtengine/lcp.h | 31 ++-- 2 files changed, 212 insertions(+), 220 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index ff53ad30e..a870b6212 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -34,21 +34,21 @@ using namespace rtengine; LCPModelCommon::LCPModelCommon() : - foc_len_x(-1.0f), - foc_len_y(-1.0f), - img_center_x(0.5f), - img_center_y(0.5f), + foc_len_x (-1.0f), + foc_len_y (-1.0f), + img_center_x (0.5f), + img_center_y (0.5f), param{{}}, - scale_factor(1.0f), - mean_error(0.0), - bad_error(false), - x0(0.0f), - y0(0.0f), - fx(0.0f), - fy(0.0f), - rfx(0.0f), - rfy(0.0f), - vign_param{{}} +scale_factor (1.0f), + mean_error (0.0), + bad_error (false), + x0 (0.0f), + y0 (0.0f), + fx (0.0f), + fy (0.0f), + rfx (0.0f), + rfy (0.0f), +vign_param{{}} { } @@ -59,13 +59,13 @@ bool LCPModelCommon::empty() const void LCPModelCommon::print() const { - printf("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", foc_len_x, foc_len_y, img_center_x, img_center_y, scale_factor, mean_error); - printf("xy0 %g/%g fxy %g/%g\n", x0, y0, fx, fy); - printf("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); + printf ("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", foc_len_x, foc_len_y, img_center_x, img_center_y, scale_factor, mean_error); + printf ("xy0 %g/%g fxy %g/%g\n", x0, y0, fx, fy); + printf ("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); } // weighted merge two parameters -void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA) +void LCPModelCommon::merge (const LCPModelCommon& a, const LCPModelCommon& b, float facA) { const float facB = 1.0f - facA; @@ -80,7 +80,7 @@ void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, flo param[i] = facA * a.param[i] + facB * b.param[i]; } - const float param0Sqr = param[0] * param[0]; + const double param0Sqr = param[0] * param[0]; vign_param[0] = -param[0]; vign_param[1] = param0Sqr - param[1]; @@ -89,10 +89,10 @@ void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, flo } -void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) +void LCPModelCommon::prepareParams (int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) { // Mention that the Adobe technical paper has a bug here, the DMAX is handled differently for focLen and imgCenter - const int Dmax = std::max(fullWidth, fullHeight); + const int Dmax = std::max (fullWidth, fullHeight); // correct focLens if (foc_len_x < 0.0f) { // they may not be given @@ -115,8 +115,9 @@ void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLen fx = foc_len_x * Dmax; fy = foc_len_y * Dmax; } - rfx = 1.0f / fx; - rfy = 1.0f / fy; + + rfx = 1.0 / fx; // calculatiom with double precision doesn't cost anything at this step + rfy = 1.0 / fy; // " //printf("FW %i /X0 %g FH %i /Y0 %g %g\n",fullWidth,x0,fullHeight,y0, imgYCenter); } @@ -127,7 +128,7 @@ LCPPersModel::LCPPersModel() } // mode: 0=distortion, 1=vignette, 2=CA -bool LCPPersModel::hasModeData(int mode) const +bool LCPPersModel::hasModeData (int mode) const { return (mode == 0 && !vignette.empty() && !vignette.bad_error) || (mode == 1 && !base.empty() && !base.bad_error) || (mode == 2 && !chromRG.empty() && !chromG.empty() && !chromBG.empty() && @@ -136,36 +137,36 @@ bool LCPPersModel::hasModeData(int mode) const void LCPPersModel::print() const { - printf("--- PersModel focLen %g; focDist %g; aperture %g\n", focLen, focDist, aperture); - printf("Base:\n"); + printf ("--- PersModel focLen %g; focDist %g; aperture %g\n", focLen, focDist, aperture); + printf ("Base:\n"); base.print(); if (!chromRG.empty()) { - printf("ChromRG:\n"); + printf ("ChromRG:\n"); chromRG.print(); } if (!chromG.empty()) { - printf("ChromG:\n"); + printf ("ChromG:\n"); chromG.print(); } if (!chromBG.empty()) { - printf("ChromBG:\n"); + printf ("ChromBG:\n"); chromBG.print(); } if (!vignette.empty()) { - printf("Vignette:\n"); + printf ("Vignette:\n"); vignette.print(); } - printf("\n"); + printf ("\n"); } // if !vignette then geometric and CA -LCPMapper::LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, - int fullWidth, int fullHeight, const CoarseTransformParams& coarse, int rawRotationDeg) +LCPMapper::LCPMapper (LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, + int fullWidth, int fullHeight, const CoarseTransformParams& coarse, int rawRotationDeg) { if (pProf == nullptr) { return; @@ -186,21 +187,21 @@ LCPMapper::LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm bool mirrorY = (rot == 180 || rot == 270); //printf("Vign: %i, fullWidth: %i/%i, focLen %g SwapXY: %i / MirX/Y %i / %i on rot:%i from %i\n",vignette, fullWidth, fullHeight, focalLength, swapXY, mirrorX, mirrorY, rot, rawRotationDeg); - pProf->calcParams(vignette ? 0 : 1, focalLength, focusDist, aperture, &mc, nullptr, nullptr); - mc.prepareParams(fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); + pProf->calcParams (vignette ? 0 : 1, focalLength, focusDist, aperture, &mc, nullptr, nullptr); + mc.prepareParams (fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); if (!vignette) { - pProf->calcParams(2, focalLength, focusDist, aperture, &chrom[0], &chrom[1], &chrom[2]); + pProf->calcParams (2, focalLength, focusDist, aperture, &chrom[0], &chrom[1], &chrom[2]); for (int i = 0; i < 3; i++) { - chrom[i].prepareParams(fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); + chrom[i].prepareParams (fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); } } enableCA = !vignette && focusDist > 0; } -void LCPMapper::correctDistortion(double& x, double& y) const +void LCPMapper::correctDistortion (double& x, double& y) const { double xd = (x - mc.x0) / mc.fx, yd = (y - mc.y0) / mc.fy; @@ -218,7 +219,7 @@ void LCPMapper::correctDistortion(double& x, double& y) const y = ynew * mc.fy + mc.y0; } -void LCPMapper::correctCA(double& x, double& y, int channel) const +void LCPMapper::correctCA (double& x, double& y, int channel) const { if (!enableCA) { return; @@ -265,47 +266,39 @@ void LCPMapper::correctCA(double& x, double& y, int channel) const } } -float LCPMapper::calcVignetteFac(int x, int y) const -{ - // No need for swapXY, since vignette is in RAW and always before rotation - float xd = ((float)x - mc.x0) * mc.rfx, yd = ((float)y - mc.y0) * mc.rfy; - - const LCPModelCommon::VignParam vignParam = mc.vign_param; - float rsqr = xd * xd + yd * yd; - - return rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); -} - -SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) const +SSEFUNCTION void LCPMapper::processVignetteLine (int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation float yd = ((float)y - mc.y0) * mc.rfy; yd *= yd; int x = 0; #ifdef __SSE2__ - const vfloat fourv = F2V(4.f); - const vfloat zerov = F2V(0.f); - const vfloat ydv = F2V(yd); - const vfloat p0 = F2V(mc.vign_param[0]); - const vfloat p1 = F2V(mc.vign_param[1]); - const vfloat p2 = F2V(mc.vign_param[2]); - const vfloat p3 = F2V(mc.vign_param[3]); - const vfloat x0v = F2V(mc.x0); - const vfloat rfxv = F2V(mc.rfx); + const vfloat fourv = F2V (4.f); + const vfloat zerov = F2V (0.f); + const vfloat ydv = F2V (yd); + const vfloat p0 = F2V (mc.vign_param[0]); + const vfloat p1 = F2V (mc.vign_param[1]); + const vfloat p2 = F2V (mc.vign_param[2]); + const vfloat p3 = F2V (mc.vign_param[3]); + const vfloat x0v = F2V (mc.x0); + const vfloat rfxv = F2V (mc.rfx); - vfloat xv = _mm_setr_ps(0.f, 1.f, 2.f, 3.f); - for (; x < width-3; x+=4) { + vfloat xv = _mm_setr_ps (0.f, 1.f, 2.f, 3.f); + + for (; x < width - 3; x += 4) { vfloat xdv = (xv - x0v) * rfxv; vfloat rsqr = xdv * xdv + ydv; vfloat vignFactorv = rsqr * (p0 + rsqr * (p1 - p2 * rsqr + p3 * rsqr * rsqr)); - vfloat valv = LVFU(line[x]); - valv += valv * vselfzero(vmaskf_gt(valv, zerov), vignFactorv); - STVFU(line[x], valv); + vfloat valv = LVFU (line[x]); + valv += valv * vselfzero (vmaskf_gt (valv, zerov), vignFactorv); + STVFU (line[x], valv); xv += fourv; } + #endif // __SSE2__ + for (; x < width; x++) { - if (line[x] > 0) { + if (line[x] > 0.f) { float xd = ((float)x - mc.x0) * mc.rfx; const LCPModelCommon::VignParam vignParam = mc.vign_param; float rsqr = xd * xd + yd; @@ -314,39 +307,40 @@ SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) c } } -SSEFUNCTION void LCPMapper::processVignetteLine3Channels(int width, int y, float *line) const +void LCPMapper::processVignetteLine3Channels (int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation float yd = ((float)y - mc.y0) * mc.rfy; yd *= yd; const LCPModelCommon::VignParam vignParam = mc.vign_param; + for (int x = 0; x < width; x++) { float xd = ((float)x - mc.x0) * mc.rfx; float rsqr = xd * xd + yd; float vignetteFactor = rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); - for(int c = 0;c < 3; ++c) { - if (line[3*x+c] > 0) { - line[3*x+c] += line[3*x+c] * vignetteFactor; + + for (int c = 0; c < 3; ++c) { + if (line[3 * x + c] > 0) { + line[3 * x + c] += line[3 * x + c] * vignetteFactor; } } } } - -LCPProfile::LCPProfile(const Glib::ustring &fname) +LCPProfile::LCPProfile (const Glib::ustring &fname) { const int BufferSize = 8192; char buf[BufferSize]; - XML_Parser parser = XML_ParserCreate(nullptr); + XML_Parser parser = XML_ParserCreate (nullptr); if (!parser) { throw "Couldn't allocate memory for XML parser"; } - XML_SetElementHandler(parser, XmlStartHandler, XmlEndHandler); - XML_SetCharacterDataHandler(parser, XmlTextHandler); - XML_SetUserData(parser, (void *)this); + XML_SetElementHandler (parser, XmlStartHandler, XmlEndHandler); + XML_SetCharacterDataHandler (parser, XmlTextHandler); + XML_SetUserData (parser, (void *)this); isFisheye = inCamProfiles = firstLIDone = inPerspect = inAlternateLensID = inAlternateLensNames = false; @@ -359,51 +353,51 @@ LCPProfile::LCPProfile(const Glib::ustring &fname) persModelCount = 0; *inInvalidTag = 0; - FILE *pFile = g_fopen(fname.c_str (), "rb"); + FILE *pFile = g_fopen (fname.c_str (), "rb"); bool done; do { - int bytesRead = (int)fread(buf, 1, BufferSize, pFile); - done = feof(pFile); + int bytesRead = (int)fread (buf, 1, BufferSize, pFile); + done = feof (pFile); - if (XML_Parse(parser, buf, bytesRead, done) == XML_STATUS_ERROR) { + if (XML_Parse (parser, buf, bytesRead, done) == XML_STATUS_ERROR) { throw "Invalid XML in LCP file"; } } while (!done); - fclose(pFile); + fclose (pFile); - XML_ParserFree(parser); + XML_ParserFree (parser); //printf("Parsing %s\n", fname.c_str()); // Two phase filter: first filter out the very rough ones, that distord the average a lot // force it, even if there are few frames (community profiles) - filterBadFrames(2.0, 0); + filterBadFrames (2.0, 0); // from the non-distorded, filter again on new average basis, but only if there are enough frames left - filterBadFrames(1.5, 100); + filterBadFrames (1.5, 100); } // from all frames not marked as bad already, take average and filter out frames with higher deviation than this if there are enough values -int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) +int LCPProfile::filterBadFrames (double maxAvgDevFac, int minFramesLeft) { // take average error per type, then calculated the maximum deviation allowed double errBase = 0, errChrom = 0, errVignette = 0; int baseCount = 0, chromCount = 0, vignetteCount = 0; for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { - if (aPersModel[pm]->hasModeData(0)) { + if (aPersModel[pm]->hasModeData (0)) { errVignette += aPersModel[pm]->vignette.mean_error; vignetteCount++; } - if (aPersModel[pm]->hasModeData(1)) { + if (aPersModel[pm]->hasModeData (1)) { errBase += aPersModel[pm]->base.mean_error; baseCount++; } - if (aPersModel[pm]->hasModeData(2)) { - errChrom += std::max(std::max(aPersModel[pm]->chromRG.mean_error, aPersModel[pm]->chromG.mean_error), aPersModel[pm]->chromBG.mean_error); + if (aPersModel[pm]->hasModeData (2)) { + errChrom += std::max (std::max (aPersModel[pm]->chromRG.mean_error, aPersModel[pm]->chromG.mean_error), aPersModel[pm]->chromBG.mean_error); chromCount++; } } @@ -426,17 +420,17 @@ int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) // Now mark all the bad ones as bad, and hasModeData will return false; for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { - if (aPersModel[pm]->hasModeData(0) && aPersModel[pm]->vignette.mean_error > maxAvgDevFac * errVignette) { + if (aPersModel[pm]->hasModeData (0) && aPersModel[pm]->vignette.mean_error > maxAvgDevFac * errVignette) { aPersModel[pm]->vignette.bad_error = true; filtered++; } - if (aPersModel[pm]->hasModeData(1) && aPersModel[pm]->base.mean_error > maxAvgDevFac * errBase) { + if (aPersModel[pm]->hasModeData (1) && aPersModel[pm]->base.mean_error > maxAvgDevFac * errBase) { aPersModel[pm]->base.bad_error = true; filtered++; } - if (aPersModel[pm]->hasModeData(2) && + if (aPersModel[pm]->hasModeData (2) && (aPersModel[pm]->chromRG.mean_error > maxAvgDevFac * errChrom || aPersModel[pm]->chromG.mean_error > maxAvgDevFac * errChrom || aPersModel[pm]->chromBG.mean_error > maxAvgDevFac * errChrom)) { aPersModel[pm]->chromRG.bad_error = aPersModel[pm]->chromG.bad_error = aPersModel[pm]->chromBG.bad_error = true; @@ -450,23 +444,22 @@ int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) return filtered; } - // mode: 0=vignette, 1=distortion, 2=CA -void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const +void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const { - float euler = exp(1.0); + float euler = exp (1.0); // find the frames with the least distance, focal length wise LCPPersModel *pLow = nullptr, *pHigh = nullptr; - float focalLengthLog = log(focalLength); //, apertureLog=aperture>0 ? log(aperture) : 0; - float focusDistLog = focusDist > 0 ? log(focusDist) + euler : 0; + float focalLengthLog = log (focalLength); //, apertureLog=aperture>0 ? log(aperture) : 0; + float focusDistLog = focusDist > 0 ? log (focusDist) + euler : 0; // Pass 1: determining best focal length, if possible different focusDistances (for the focDist is not given case) for (int pm = 0; pm < persModelCount; pm++) { float f = aPersModel[pm]->focLen; - if (aPersModel[pm]->hasModeData(mode)) { + if (aPersModel[pm]->hasModeData (mode)) { if (f <= focalLength && (pLow == nullptr || f > pLow->focLen || (focusDist == 0 && f == pLow->focLen && pLow->focDist > aPersModel[pm]->focDist))) { pLow = aPersModel[pm]; } @@ -490,10 +483,10 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float for (int pm = 0; pm < persModelCount; pm++) { float aper = aPersModel[pm]->aperture; // float aperLog=log(aper); float focDist = aPersModel[pm]->focDist; - float focDistLog = log(focDist) + euler; + float focDistLog = log (focDist) + euler; double meanErr; - if (aPersModel[pm]->hasModeData(mode)) { + if (aPersModel[pm]->hasModeData (mode)) { if (mode == 0) { meanErr = aPersModel[pm]->vignette.mean_error; @@ -502,14 +495,14 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float if (aPersModel[pm]->focLen == bestFocLenLow && ( (aper == aperture && pLow->vignette.mean_error > meanErr) || (aper >= aperture && aper < pLow->aperture && pLow->aperture > aperture) - || (aper <= aperture && (pLow->aperture > aperture || fabs(aperture - aper) < fabs(aperture - pLow->aperture))))) { + || (aper <= aperture && (pLow->aperture > aperture || fabs (aperture - aper) < fabs (aperture - pLow->aperture))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( (aper == aperture && pHigh->vignette.mean_error > meanErr) || (aper <= aperture && aper > pHigh->aperture && pHigh->aperture < aperture) - || (aper >= aperture && (pHigh->aperture < aperture || fabs(aperture - aper) < fabs(aperture - pHigh->aperture))))) { + || (aper >= aperture && (pHigh->aperture < aperture || fabs (aperture - aper) < fabs (aperture - pHigh->aperture))))) { pHigh = aPersModel[pm]; } } else { @@ -520,14 +513,14 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float if (aPersModel[pm]->focLen == bestFocLenLow && ( (focDist == focusDist && (mode == 1 ? pLow->base.mean_error : pLow->chromG.mean_error) > meanErr) || (focDist >= focusDist && focDist < pLow->focDist && pLow->focDist > focusDist) - || (focDist <= focusDist && (pLow->focDist > focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pLow->focDist) + euler)))))) { + || (focDist <= focusDist && (pLow->focDist > focusDist || fabs (focusDistLog - focDistLog) < fabs (focusDistLog - (log (pLow->focDist) + euler)))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( (focDist == focusDist && (mode == 1 ? pHigh->base.mean_error : pHigh->chromG.mean_error) > meanErr) || (focDist <= focusDist && focDist > pHigh->focDist && pHigh->focDist < focusDist) - || (focDist >= focusDist && (pHigh->focDist < focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pHigh->focDist) + euler)))))) { + || (focDist >= focusDist && (pHigh->focDist < focusDist || fabs (focusDistLog - focDistLog) < fabs (focusDistLog - (log (pHigh->focDist) + euler)))))) { pHigh = aPersModel[pm]; } } else { @@ -552,7 +545,7 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float // There is as foclen range, take that as basis if (pLow->focLen < pHigh->focLen) { - facLow = (log(pHigh->focLen) - focalLengthLog) / (log(pHigh->focLen) - log(pLow->focLen)); + facLow = (log (pHigh->focLen) - focalLengthLog) / (log (pHigh->focLen) - log (pLow->focLen)); } else { focLenOnSpot = pLow->focLen == pHigh->focLen && pLow->focLen == focalLength; } @@ -564,45 +557,45 @@ void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float facLow = focLenOnSpot ? facAperLow : (0.5 * facLow + 0.5 * facAperLow); } else if (mode != 0 && focusDist > 0 && pLow->focDist < focusDist && pHigh->focDist > focusDist) { // focus distance for all else (if focus distance is given) - float facDistLow = (log(pHigh->focDist) + euler - focusDistLog) / (log(pHigh->focDist) - log(pLow->focDist)); + float facDistLow = (log (pHigh->focDist) + euler - focusDistLog) / (log (pHigh->focDist) - log (pLow->focDist)); facLow = focLenOnSpot ? facDistLow : (0.8 * facLow + 0.2 * facDistLow); } switch (mode) { - case 0: // vignette - pCorr1->merge(pLow->vignette, pHigh->vignette, facLow); - break; + case 0: // vignette + pCorr1->merge (pLow->vignette, pHigh->vignette, facLow); + break; - case 1: // distortion - pCorr1->merge(pLow->base, pHigh->base, facLow); - break; + case 1: // distortion + pCorr1->merge (pLow->base, pHigh->base, facLow); + break; - case 2: // CA - pCorr1->merge(pLow->chromRG, pHigh->chromRG, facLow); - pCorr2->merge(pLow->chromG, pHigh->chromG, facLow); - pCorr3->merge(pLow->chromBG, pHigh->chromBG, facLow); - break; + case 2: // CA + pCorr1->merge (pLow->chromRG, pHigh->chromRG, facLow); + pCorr2->merge (pLow->chromG, pHigh->chromG, facLow); + pCorr3->merge (pLow->chromBG, pHigh->chromBG, facLow); + break; } //printf("LCP mode=%i, dist: %g found frames: Fno %g-%g; FocLen %g-%g; Dist %g-%g with weight %g\n", mode, focusDist, pLow->aperture, pHigh->aperture, pLow->focLen, pHigh->focLen, pLow->focDist, pHigh->focDist, facLow); } else { - printf("Error: LCP file contained no %s parameters\n", mode == 0 ? "vignette" : mode == 1 ? "distortion" : "CA" ); + printf ("Error: LCP file contained no %s parameters\n", mode == 0 ? "vignette" : mode == 1 ? "distortion" : "CA" ); } } void LCPProfile::print() const { - printf("=== Profile %s\n", profileName.c_str()); - printf("Frames: %i, RAW: %i; Fisheye: %i; Sensorformat: %f\n", persModelCount, isRaw, isFisheye, sensorFormatFactor); + printf ("=== Profile %s\n", profileName.c_str()); + printf ("Frames: %i, RAW: %i; Fisheye: %i; Sensorformat: %f\n", persModelCount, isRaw, isFisheye, sensorFormatFactor); for (int pm = 0; pm < persModelCount; pm++) { aPersModel[pm]->print(); } } -void XMLCALL LCPProfile::XmlStartHandler(void *pLCPProfile, const char *el, const char **attr) +void XMLCALL LCPProfile::XmlStartHandler (void *pLCPProfile, const char *el, const char **attr) { - LCPProfile *pProf = static_cast(pLCPProfile); + LCPProfile *pProf = static_cast (pLCPProfile); bool parseAttr = false; if (*pProf->inInvalidTag) { @@ -610,29 +603,29 @@ void XMLCALL LCPProfile::XmlStartHandler(void *pLCPProfile, const char *el, cons } // clean up tagname - const char* src = strrchr(el, ':'); + const char* src = strrchr (el, ':'); if (src == nullptr) { - src = const_cast(el); + src = const_cast (el); } else { src++; } - strcpy(pProf->lastTag, src); + strcpy (pProf->lastTag, src); - if (!strcmp("VignetteModelPiecewiseParam", src)) { - strcpy(pProf->inInvalidTag, src); + if (!strcmp ("VignetteModelPiecewiseParam", src)) { + strcpy (pProf->inInvalidTag, src); } - if (!strcmp("CameraProfiles", src)) { + if (!strcmp ("CameraProfiles", src)) { pProf->inCamProfiles = true; } - if (!strcmp("AlternateLensIDs", src)) { + if (!strcmp ("AlternateLensIDs", src)) { pProf->inAlternateLensID = true; } - if (!strcmp("AlternateLensNames", src)) { + if (!strcmp ("AlternateLensNames", src)) { pProf->inAlternateLensNames = true; } @@ -640,37 +633,37 @@ void XMLCALL LCPProfile::XmlStartHandler(void *pLCPProfile, const char *el, cons return; } - if (!strcmp("li", src)) { + if (!strcmp ("li", src)) { pProf->pCurPersModel = new LCPPersModel(); pProf->pCurCommon = &pProf->pCurPersModel->base; // iterated to next tags within persModel return; } - if (!strcmp("PerspectiveModel", src)) { + if (!strcmp ("PerspectiveModel", src)) { pProf->firstLIDone = true; pProf->inPerspect = true; return; - } else if (!strcmp("FisheyeModel", src)) { + } else if (!strcmp ("FisheyeModel", src)) { pProf->firstLIDone = true; pProf->inPerspect = true; pProf->isFisheye = true; // just misses third param, and different path, rest is the same return; - } else if (!strcmp("Description", src)) { + } else if (!strcmp ("Description", src)) { parseAttr = true; } // Move pointer to general section if (pProf->inPerspect) { - if (!strcmp("ChromaticRedGreenModel", src)) { + if (!strcmp ("ChromaticRedGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromRG; parseAttr = true; - } else if (!strcmp("ChromaticGreenModel", src)) { + } else if (!strcmp ("ChromaticGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromG; parseAttr = true; - } else if (!strcmp("ChromaticBlueGreenModel", src)) { + } else if (!strcmp ("ChromaticBlueGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromBG; parseAttr = true; - } else if (!strcmp("VignetteModel", src)) { + } else if (!strcmp ("VignetteModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->vignette; parseAttr = true; } @@ -680,23 +673,23 @@ void XMLCALL LCPProfile::XmlStartHandler(void *pLCPProfile, const char *el, cons // simulate tags by feeding them in if (parseAttr && attr != nullptr) { for (int i = 0; attr[i]; i += 2) { - const char* nameStart = strrchr(attr[i], ':'); + const char* nameStart = strrchr (attr[i], ':'); if (nameStart == nullptr) { - nameStart = const_cast(attr[i]); + nameStart = const_cast (attr[i]); } else { nameStart++; } - strcpy(pProf->lastTag, nameStart); - XmlTextHandler(pLCPProfile, attr[i + 1], strlen(attr[i + 1])); + strcpy (pProf->lastTag, nameStart); + XmlTextHandler (pLCPProfile, attr[i + 1], strlen (attr[i + 1])); } } } -void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, int len) +void XMLCALL LCPProfile::XmlTextHandler (void *pLCPProfile, const XML_Char *s, int len) { - LCPProfile *pProf = static_cast(pLCPProfile); + LCPProfile *pProf = static_cast (pLCPProfile); if (!pProf->inCamProfiles || pProf->inAlternateLensID || pProf->inAlternateLensNames || *pProf->inInvalidTag) { return; @@ -707,7 +700,7 @@ void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, in int i = 0; while (i < len && onlyWhiteSpace) { - onlyWhiteSpace = isspace(s[i]); + onlyWhiteSpace = isspace (s[i]); i++; } @@ -717,32 +710,32 @@ void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, in // convert to null terminated char raw[len + 1]; - memcpy(raw, s, len); + memcpy (raw, s, len); raw[len] = 0; char* tag = pProf->lastTag; // Common data section if (!pProf->firstLIDone) { // Generic tags are the same for all - if (!strcmp("ProfileName", tag)) { - pProf->profileName = Glib::ustring(raw); - } else if (!strcmp("Model", tag)) { - pProf->camera = Glib::ustring(raw); - } else if (!strcmp("Lens", tag)) { - pProf->lens = Glib::ustring(raw); - } else if (!strcmp("CameraPrettyName", tag)) { - pProf->cameraPrettyName = Glib::ustring(raw); - } else if (!strcmp("LensPrettyName", tag)) { - pProf->lensPrettyName = Glib::ustring(raw); - } else if (!strcmp("CameraRawProfile", tag)) { - pProf->isRaw = !strcmp("True", raw); + if (!strcmp ("ProfileName", tag)) { + pProf->profileName = Glib::ustring (raw); + } else if (!strcmp ("Model", tag)) { + pProf->camera = Glib::ustring (raw); + } else if (!strcmp ("Lens", tag)) { + pProf->lens = Glib::ustring (raw); + } else if (!strcmp ("CameraPrettyName", tag)) { + pProf->cameraPrettyName = Glib::ustring (raw); + } else if (!strcmp ("LensPrettyName", tag)) { + pProf->lensPrettyName = Glib::ustring (raw); + } else if (!strcmp ("CameraRawProfile", tag)) { + pProf->isRaw = !strcmp ("True", raw); } } // --- Now all floating points. Must replace local dot characters // WARNING: called by different threads, that may run on different local settings, // so don't use system params - if (atof("1,2345") == 1.2345) { + if (atof ("1,2345") == 1.2345) { char* p = raw; while (*p) { @@ -755,69 +748,69 @@ void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, in } if (!pProf->firstLIDone) { - if (!strcmp("SensorFormatFactor", tag)) { - pProf->sensorFormatFactor = atof(raw); + if (!strcmp ("SensorFormatFactor", tag)) { + pProf->sensorFormatFactor = atof (raw); } } // Perspective model base data - if (!strcmp("FocalLength", tag)) { - pProf->pCurPersModel->focLen = atof(raw); - } else if (!strcmp("FocusDistance", tag)) { - double focDist = atof(raw); + if (!strcmp ("FocalLength", tag)) { + pProf->pCurPersModel->focLen = atof (raw); + } else if (!strcmp ("FocusDistance", tag)) { + double focDist = atof (raw); pProf->pCurPersModel->focDist = focDist < 10000 ? focDist : 10000; - } else if (!strcmp("ApertureValue", tag)) { - pProf->pCurPersModel->aperture = atof(raw); + } else if (!strcmp ("ApertureValue", tag)) { + pProf->pCurPersModel->aperture = atof (raw); } // Section depended - if (!strcmp("FocalLengthX", tag)) { - pProf->pCurCommon->foc_len_x = atof(raw); - } else if (!strcmp("FocalLengthY", tag)) { - pProf->pCurCommon->foc_len_y = atof(raw); - } else if (!strcmp("ImageXCenter", tag)) { - pProf->pCurCommon->img_center_x = atof(raw); - } else if (!strcmp("ImageYCenter", tag)) { - pProf->pCurCommon->img_center_y = atof(raw); - } else if (!strcmp("ScaleFactor", tag)) { - pProf->pCurCommon->scale_factor = atof(raw); - } else if (!strcmp("ResidualMeanError", tag)) { - pProf->pCurCommon->mean_error = atof(raw); - } else if (!strcmp("RadialDistortParam1", tag) || !strcmp("VignetteModelParam1", tag)) { - pProf->pCurCommon->param[0] = atof(raw); - } else if (!strcmp("RadialDistortParam2", tag) || !strcmp("VignetteModelParam2", tag)) { - pProf->pCurCommon->param[1] = atof(raw); - } else if (!strcmp("RadialDistortParam3", tag) || !strcmp("VignetteModelParam3", tag)) { - pProf->pCurCommon->param[2] = atof(raw); - } else if (!strcmp("RadialDistortParam4", tag) || !strcmp("TangentialDistortParam1", tag)) { - pProf->pCurCommon->param[3] = atof(raw); - } else if (!strcmp("RadialDistortParam5", tag) || !strcmp("TangentialDistortParam2", tag)) { - pProf->pCurCommon->param[4] = atof(raw); + if (!strcmp ("FocalLengthX", tag)) { + pProf->pCurCommon->foc_len_x = atof (raw); + } else if (!strcmp ("FocalLengthY", tag)) { + pProf->pCurCommon->foc_len_y = atof (raw); + } else if (!strcmp ("ImageXCenter", tag)) { + pProf->pCurCommon->img_center_x = atof (raw); + } else if (!strcmp ("ImageYCenter", tag)) { + pProf->pCurCommon->img_center_y = atof (raw); + } else if (!strcmp ("ScaleFactor", tag)) { + pProf->pCurCommon->scale_factor = atof (raw); + } else if (!strcmp ("ResidualMeanError", tag)) { + pProf->pCurCommon->mean_error = atof (raw); + } else if (!strcmp ("RadialDistortParam1", tag) || !strcmp ("VignetteModelParam1", tag)) { + pProf->pCurCommon->param[0] = atof (raw); + } else if (!strcmp ("RadialDistortParam2", tag) || !strcmp ("VignetteModelParam2", tag)) { + pProf->pCurCommon->param[1] = atof (raw); + } else if (!strcmp ("RadialDistortParam3", tag) || !strcmp ("VignetteModelParam3", tag)) { + pProf->pCurCommon->param[2] = atof (raw); + } else if (!strcmp ("RadialDistortParam4", tag) || !strcmp ("TangentialDistortParam1", tag)) { + pProf->pCurCommon->param[3] = atof (raw); + } else if (!strcmp ("RadialDistortParam5", tag) || !strcmp ("TangentialDistortParam2", tag)) { + pProf->pCurCommon->param[4] = atof (raw); } } -void XMLCALL LCPProfile::XmlEndHandler(void *pLCPProfile, const char *el) +void XMLCALL LCPProfile::XmlEndHandler (void *pLCPProfile, const char *el) { - LCPProfile *pProf = static_cast(pLCPProfile); + LCPProfile *pProf = static_cast (pLCPProfile); // We ignore everything in dirty tag till it's gone if (*pProf->inInvalidTag) { - if (strstr(el, pProf->inInvalidTag)) { + if (strstr (el, pProf->inInvalidTag)) { *pProf->inInvalidTag = 0; } return; } - if (strstr(el, ":CameraProfiles")) { + if (strstr (el, ":CameraProfiles")) { pProf->inCamProfiles = false; } - if (strstr(el, ":AlternateLensIDs")) { + if (strstr (el, ":AlternateLensIDs")) { pProf->inAlternateLensID = false; } - if (strstr(el, ":AlternateLensNames")) { + if (strstr (el, ":AlternateLensNames")) { pProf->inAlternateLensNames = false; } @@ -825,9 +818,9 @@ void XMLCALL LCPProfile::XmlEndHandler(void *pLCPProfile, const char *el) return; } - if (strstr(el, ":PerspectiveModel") || strstr(el, ":FisheyeModel")) { + if (strstr (el, ":PerspectiveModel") || strstr (el, ":FisheyeModel")) { pProf->inPerspect = false; - } else if (strstr(el, ":li")) { + } else if (strstr (el, ":li")) { pProf->aPersModel[pProf->persModelCount] = pProf->pCurPersModel; pProf->pCurPersModel = nullptr; pProf->persModelCount++; @@ -843,11 +836,11 @@ LCPStore* LCPStore::getInstance() LCPProfile* LCPStore::getProfile (Glib::ustring filename) { - if (filename.length() == 0 || !isValidLCPFileName(filename)) { + if (filename.length() == 0 || !isValidLCPFileName (filename)) { return nullptr; } - MyMutex::MyLock lock(mtx); + MyMutex::MyLock lock (mtx); std::map::iterator r = profileCache.find (filename); @@ -856,12 +849,12 @@ LCPProfile* LCPStore::getProfile (Glib::ustring filename) } // Add profile (if exists) - profileCache[filename] = new LCPProfile(filename); + profileCache[filename] = new LCPProfile (filename); //profileCache[filename]->print(); return profileCache[filename]; } -bool LCPStore::isValidLCPFileName(Glib::ustring filename) const +bool LCPStore::isValidLCPFileName (Glib::ustring filename) const { if (!Glib::file_test (filename, Glib::FILE_TEST_EXISTS) || Glib::file_test (filename, Glib::FILE_TEST_IS_DIR)) { return false; @@ -879,10 +872,10 @@ Glib::ustring LCPStore::getDefaultCommonDirectory() const WCHAR pathW[MAX_PATH] = {0}; char pathA[MAX_PATH]; - if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_COMMON_APPDATA, false)) { + if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_COMMON_APPDATA, false)) { char pathA[MAX_PATH]; - WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0); - Glib::ustring fullDir = Glib::ustring(pathA) + Glib::ustring("\\Adobe\\CameraRaw\\LensProfiles\\1.0"); + WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0); + Glib::ustring fullDir = Glib::ustring (pathA) + Glib::ustring ("\\Adobe\\CameraRaw\\LensProfiles\\1.0"); if (Glib::file_test (fullDir, Glib::FILE_TEST_IS_DIR)) { dir = fullDir; diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 92c984921..9488f74d7 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -39,8 +39,8 @@ public: LCPModelCommon(); bool empty() const; // is it empty void print() const; // printf all values - void merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA); - void prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY); + void merge (const LCPModelCommon& a, const LCPModelCommon& b, float facA); + void prepareParams (int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY); //private: using Param = std::array; @@ -75,7 +75,7 @@ public: LCPModelCommon vignette; // vignette (may be empty) LCPPersModel(); - bool hasModeData(int mode) const; + bool hasModeData (int mode) const; void print() const; }; @@ -88,11 +88,11 @@ class LCPProfile LCPPersModel* pCurPersModel; LCPModelCommon* pCurCommon; - static void XMLCALL XmlStartHandler(void *pLCPProfile, const char *el, const char **attr); + static void XMLCALL XmlStartHandler (void *pLCPProfile, const char *el, const char **attr); static void XMLCALL XmlTextHandler (void *pLCPProfile, const XML_Char *s, int len); static void XMLCALL XmlEndHandler (void *pLCPProfile, const char *el); - int filterBadFrames(double maxAvgDevFac, int minFramesLeft); + int filterBadFrames (double maxAvgDevFac, int minFramesLeft); public: // Common data @@ -105,9 +105,9 @@ public: static const int MaxPersModelCount = 3000; LCPPersModel* aPersModel[MaxPersModelCount]; // Do NOT use std::list or something, it's buggy in GCC! - explicit LCPProfile(const Glib::ustring &fname); + explicit LCPProfile (const Glib::ustring &fname); - void calcParams(int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const; // Interpolates between the persModels frames + void calcParams (int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const; // Interpolates between the persModels frames void print() const; }; @@ -121,8 +121,8 @@ class LCPStore public: Glib::ustring getDefaultCommonDirectory() const; - bool isValidLCPFileName(Glib::ustring filename) const; - LCPProfile* getProfile(Glib::ustring filename); + bool isValidLCPFileName (Glib::ustring filename) const; + LCPProfile* getProfile (Glib::ustring filename); static LCPStore* getInstance(); }; @@ -143,14 +143,13 @@ public: bool enableCA; // is the mapper capable if CA correction? // precalculates the mapper. - LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, int fullWidth, int fullHeight, - const CoarseTransformParams& coarse, int rawRotationDeg); + LCPMapper (LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, int fullWidth, int fullHeight, + const CoarseTransformParams& coarse, int rawRotationDeg); - void correctDistortion(double& x, double& y) const; // MUST be the first stage - void correctCA(double& x, double& y, int channel) const; - float calcVignetteFac (int x, int y) const; // MUST be in RAW - void processVignetteLine(int width, int y, float *line) const; - void processVignetteLine3Channels(int width, int y, float *line) const; + void correctDistortion (double& x, double& y) const; // MUST be the first stage + void correctCA (double& x, double& y, int channel) const; + void processVignetteLine (int width, int y, float *line) const; + void processVignetteLine3Channels (int width, int y, float *line) const; }; } From 8e205afeed8c1936bb88cac9339be21c1448ff39 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Wed, 22 Feb 2017 14:43:41 +0100 Subject: [PATCH 5/6] Revert "Further cleanup and astyled lcp.*" This reverts commit 1348ea06e4975d26b50f9e723ae61f87916dcf17. --- rtengine/lcp.cc | 401 ++++++++++++++++++++++++------------------------ rtengine/lcp.h | 31 ++-- 2 files changed, 220 insertions(+), 212 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index a870b6212..ff53ad30e 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -34,21 +34,21 @@ using namespace rtengine; LCPModelCommon::LCPModelCommon() : - foc_len_x (-1.0f), - foc_len_y (-1.0f), - img_center_x (0.5f), - img_center_y (0.5f), + foc_len_x(-1.0f), + foc_len_y(-1.0f), + img_center_x(0.5f), + img_center_y(0.5f), param{{}}, -scale_factor (1.0f), - mean_error (0.0), - bad_error (false), - x0 (0.0f), - y0 (0.0f), - fx (0.0f), - fy (0.0f), - rfx (0.0f), - rfy (0.0f), -vign_param{{}} + scale_factor(1.0f), + mean_error(0.0), + bad_error(false), + x0(0.0f), + y0(0.0f), + fx(0.0f), + fy(0.0f), + rfx(0.0f), + rfy(0.0f), + vign_param{{}} { } @@ -59,13 +59,13 @@ bool LCPModelCommon::empty() const void LCPModelCommon::print() const { - printf ("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", foc_len_x, foc_len_y, img_center_x, img_center_y, scale_factor, mean_error); - printf ("xy0 %g/%g fxy %g/%g\n", x0, y0, fx, fy); - printf ("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); + printf("focLen %g/%g; imgCenter %g/%g; scale %g; err %g\n", foc_len_x, foc_len_y, img_center_x, img_center_y, scale_factor, mean_error); + printf("xy0 %g/%g fxy %g/%g\n", x0, y0, fx, fy); + printf("param: %g/%g/%g/%g/%g\n", param[0], param[1], param[2], param[3], param[4]); } // weighted merge two parameters -void LCPModelCommon::merge (const LCPModelCommon& a, const LCPModelCommon& b, float facA) +void LCPModelCommon::merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA) { const float facB = 1.0f - facA; @@ -80,7 +80,7 @@ void LCPModelCommon::merge (const LCPModelCommon& a, const LCPModelCommon& b, fl param[i] = facA * a.param[i] + facB * b.param[i]; } - const double param0Sqr = param[0] * param[0]; + const float param0Sqr = param[0] * param[0]; vign_param[0] = -param[0]; vign_param[1] = param0Sqr - param[1]; @@ -89,10 +89,10 @@ void LCPModelCommon::merge (const LCPModelCommon& a, const LCPModelCommon& b, fl } -void LCPModelCommon::prepareParams (int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) +void LCPModelCommon::prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY) { // Mention that the Adobe technical paper has a bug here, the DMAX is handled differently for focLen and imgCenter - const int Dmax = std::max (fullWidth, fullHeight); + const int Dmax = std::max(fullWidth, fullHeight); // correct focLens if (foc_len_x < 0.0f) { // they may not be given @@ -115,9 +115,8 @@ void LCPModelCommon::prepareParams (int fullWidth, int fullHeight, float focalLe fx = foc_len_x * Dmax; fy = foc_len_y * Dmax; } - - rfx = 1.0 / fx; // calculatiom with double precision doesn't cost anything at this step - rfy = 1.0 / fy; // " + rfx = 1.0f / fx; + rfy = 1.0f / fy; //printf("FW %i /X0 %g FH %i /Y0 %g %g\n",fullWidth,x0,fullHeight,y0, imgYCenter); } @@ -128,7 +127,7 @@ LCPPersModel::LCPPersModel() } // mode: 0=distortion, 1=vignette, 2=CA -bool LCPPersModel::hasModeData (int mode) const +bool LCPPersModel::hasModeData(int mode) const { return (mode == 0 && !vignette.empty() && !vignette.bad_error) || (mode == 1 && !base.empty() && !base.bad_error) || (mode == 2 && !chromRG.empty() && !chromG.empty() && !chromBG.empty() && @@ -137,36 +136,36 @@ bool LCPPersModel::hasModeData (int mode) const void LCPPersModel::print() const { - printf ("--- PersModel focLen %g; focDist %g; aperture %g\n", focLen, focDist, aperture); - printf ("Base:\n"); + printf("--- PersModel focLen %g; focDist %g; aperture %g\n", focLen, focDist, aperture); + printf("Base:\n"); base.print(); if (!chromRG.empty()) { - printf ("ChromRG:\n"); + printf("ChromRG:\n"); chromRG.print(); } if (!chromG.empty()) { - printf ("ChromG:\n"); + printf("ChromG:\n"); chromG.print(); } if (!chromBG.empty()) { - printf ("ChromBG:\n"); + printf("ChromBG:\n"); chromBG.print(); } if (!vignette.empty()) { - printf ("Vignette:\n"); + printf("Vignette:\n"); vignette.print(); } - printf ("\n"); + printf("\n"); } // if !vignette then geometric and CA -LCPMapper::LCPMapper (LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, - int fullWidth, int fullHeight, const CoarseTransformParams& coarse, int rawRotationDeg) +LCPMapper::LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, + int fullWidth, int fullHeight, const CoarseTransformParams& coarse, int rawRotationDeg) { if (pProf == nullptr) { return; @@ -187,21 +186,21 @@ LCPMapper::LCPMapper (LCPProfile* pProf, float focalLength, float focalLength35m bool mirrorY = (rot == 180 || rot == 270); //printf("Vign: %i, fullWidth: %i/%i, focLen %g SwapXY: %i / MirX/Y %i / %i on rot:%i from %i\n",vignette, fullWidth, fullHeight, focalLength, swapXY, mirrorX, mirrorY, rot, rawRotationDeg); - pProf->calcParams (vignette ? 0 : 1, focalLength, focusDist, aperture, &mc, nullptr, nullptr); - mc.prepareParams (fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); + pProf->calcParams(vignette ? 0 : 1, focalLength, focusDist, aperture, &mc, nullptr, nullptr); + mc.prepareParams(fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); if (!vignette) { - pProf->calcParams (2, focalLength, focusDist, aperture, &chrom[0], &chrom[1], &chrom[2]); + pProf->calcParams(2, focalLength, focusDist, aperture, &chrom[0], &chrom[1], &chrom[2]); for (int i = 0; i < 3; i++) { - chrom[i].prepareParams (fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); + chrom[i].prepareParams(fullWidth, fullHeight, focalLength, focalLength35mm, pProf->sensorFormatFactor, swapXY, mirrorX, mirrorY); } } enableCA = !vignette && focusDist > 0; } -void LCPMapper::correctDistortion (double& x, double& y) const +void LCPMapper::correctDistortion(double& x, double& y) const { double xd = (x - mc.x0) / mc.fx, yd = (y - mc.y0) / mc.fy; @@ -219,7 +218,7 @@ void LCPMapper::correctDistortion (double& x, double& y) const y = ynew * mc.fy + mc.y0; } -void LCPMapper::correctCA (double& x, double& y, int channel) const +void LCPMapper::correctCA(double& x, double& y, int channel) const { if (!enableCA) { return; @@ -266,39 +265,47 @@ void LCPMapper::correctCA (double& x, double& y, int channel) const } } -SSEFUNCTION void LCPMapper::processVignetteLine (int width, int y, float *line) const +float LCPMapper::calcVignetteFac(int x, int y) const +{ + // No need for swapXY, since vignette is in RAW and always before rotation + float xd = ((float)x - mc.x0) * mc.rfx, yd = ((float)y - mc.y0) * mc.rfy; + + const LCPModelCommon::VignParam vignParam = mc.vign_param; + float rsqr = xd * xd + yd * yd; + + return rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); +} + +SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation float yd = ((float)y - mc.y0) * mc.rfy; yd *= yd; int x = 0; #ifdef __SSE2__ - const vfloat fourv = F2V (4.f); - const vfloat zerov = F2V (0.f); - const vfloat ydv = F2V (yd); - const vfloat p0 = F2V (mc.vign_param[0]); - const vfloat p1 = F2V (mc.vign_param[1]); - const vfloat p2 = F2V (mc.vign_param[2]); - const vfloat p3 = F2V (mc.vign_param[3]); - const vfloat x0v = F2V (mc.x0); - const vfloat rfxv = F2V (mc.rfx); + const vfloat fourv = F2V(4.f); + const vfloat zerov = F2V(0.f); + const vfloat ydv = F2V(yd); + const vfloat p0 = F2V(mc.vign_param[0]); + const vfloat p1 = F2V(mc.vign_param[1]); + const vfloat p2 = F2V(mc.vign_param[2]); + const vfloat p3 = F2V(mc.vign_param[3]); + const vfloat x0v = F2V(mc.x0); + const vfloat rfxv = F2V(mc.rfx); - vfloat xv = _mm_setr_ps (0.f, 1.f, 2.f, 3.f); - - for (; x < width - 3; x += 4) { + vfloat xv = _mm_setr_ps(0.f, 1.f, 2.f, 3.f); + for (; x < width-3; x+=4) { vfloat xdv = (xv - x0v) * rfxv; vfloat rsqr = xdv * xdv + ydv; vfloat vignFactorv = rsqr * (p0 + rsqr * (p1 - p2 * rsqr + p3 * rsqr * rsqr)); - vfloat valv = LVFU (line[x]); - valv += valv * vselfzero (vmaskf_gt (valv, zerov), vignFactorv); - STVFU (line[x], valv); + vfloat valv = LVFU(line[x]); + valv += valv * vselfzero(vmaskf_gt(valv, zerov), vignFactorv); + STVFU(line[x], valv); xv += fourv; } - #endif // __SSE2__ - for (; x < width; x++) { - if (line[x] > 0.f) { + if (line[x] > 0) { float xd = ((float)x - mc.x0) * mc.rfx; const LCPModelCommon::VignParam vignParam = mc.vign_param; float rsqr = xd * xd + yd; @@ -307,40 +314,39 @@ SSEFUNCTION void LCPMapper::processVignetteLine (int width, int y, float *line) } } -void LCPMapper::processVignetteLine3Channels (int width, int y, float *line) const +SSEFUNCTION void LCPMapper::processVignetteLine3Channels(int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation float yd = ((float)y - mc.y0) * mc.rfy; yd *= yd; const LCPModelCommon::VignParam vignParam = mc.vign_param; - for (int x = 0; x < width; x++) { float xd = ((float)x - mc.x0) * mc.rfx; float rsqr = xd * xd + yd; float vignetteFactor = rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); - - for (int c = 0; c < 3; ++c) { - if (line[3 * x + c] > 0) { - line[3 * x + c] += line[3 * x + c] * vignetteFactor; + for(int c = 0;c < 3; ++c) { + if (line[3*x+c] > 0) { + line[3*x+c] += line[3*x+c] * vignetteFactor; } } } } -LCPProfile::LCPProfile (const Glib::ustring &fname) + +LCPProfile::LCPProfile(const Glib::ustring &fname) { const int BufferSize = 8192; char buf[BufferSize]; - XML_Parser parser = XML_ParserCreate (nullptr); + XML_Parser parser = XML_ParserCreate(nullptr); if (!parser) { throw "Couldn't allocate memory for XML parser"; } - XML_SetElementHandler (parser, XmlStartHandler, XmlEndHandler); - XML_SetCharacterDataHandler (parser, XmlTextHandler); - XML_SetUserData (parser, (void *)this); + XML_SetElementHandler(parser, XmlStartHandler, XmlEndHandler); + XML_SetCharacterDataHandler(parser, XmlTextHandler); + XML_SetUserData(parser, (void *)this); isFisheye = inCamProfiles = firstLIDone = inPerspect = inAlternateLensID = inAlternateLensNames = false; @@ -353,51 +359,51 @@ LCPProfile::LCPProfile (const Glib::ustring &fname) persModelCount = 0; *inInvalidTag = 0; - FILE *pFile = g_fopen (fname.c_str (), "rb"); + FILE *pFile = g_fopen(fname.c_str (), "rb"); bool done; do { - int bytesRead = (int)fread (buf, 1, BufferSize, pFile); - done = feof (pFile); + int bytesRead = (int)fread(buf, 1, BufferSize, pFile); + done = feof(pFile); - if (XML_Parse (parser, buf, bytesRead, done) == XML_STATUS_ERROR) { + if (XML_Parse(parser, buf, bytesRead, done) == XML_STATUS_ERROR) { throw "Invalid XML in LCP file"; } } while (!done); - fclose (pFile); + fclose(pFile); - XML_ParserFree (parser); + XML_ParserFree(parser); //printf("Parsing %s\n", fname.c_str()); // Two phase filter: first filter out the very rough ones, that distord the average a lot // force it, even if there are few frames (community profiles) - filterBadFrames (2.0, 0); + filterBadFrames(2.0, 0); // from the non-distorded, filter again on new average basis, but only if there are enough frames left - filterBadFrames (1.5, 100); + filterBadFrames(1.5, 100); } // from all frames not marked as bad already, take average and filter out frames with higher deviation than this if there are enough values -int LCPProfile::filterBadFrames (double maxAvgDevFac, int minFramesLeft) +int LCPProfile::filterBadFrames(double maxAvgDevFac, int minFramesLeft) { // take average error per type, then calculated the maximum deviation allowed double errBase = 0, errChrom = 0, errVignette = 0; int baseCount = 0, chromCount = 0, vignetteCount = 0; for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { - if (aPersModel[pm]->hasModeData (0)) { + if (aPersModel[pm]->hasModeData(0)) { errVignette += aPersModel[pm]->vignette.mean_error; vignetteCount++; } - if (aPersModel[pm]->hasModeData (1)) { + if (aPersModel[pm]->hasModeData(1)) { errBase += aPersModel[pm]->base.mean_error; baseCount++; } - if (aPersModel[pm]->hasModeData (2)) { - errChrom += std::max (std::max (aPersModel[pm]->chromRG.mean_error, aPersModel[pm]->chromG.mean_error), aPersModel[pm]->chromBG.mean_error); + if (aPersModel[pm]->hasModeData(2)) { + errChrom += std::max(std::max(aPersModel[pm]->chromRG.mean_error, aPersModel[pm]->chromG.mean_error), aPersModel[pm]->chromBG.mean_error); chromCount++; } } @@ -420,17 +426,17 @@ int LCPProfile::filterBadFrames (double maxAvgDevFac, int minFramesLeft) // Now mark all the bad ones as bad, and hasModeData will return false; for (int pm = 0; pm < MaxPersModelCount && aPersModel[pm]; pm++) { - if (aPersModel[pm]->hasModeData (0) && aPersModel[pm]->vignette.mean_error > maxAvgDevFac * errVignette) { + if (aPersModel[pm]->hasModeData(0) && aPersModel[pm]->vignette.mean_error > maxAvgDevFac * errVignette) { aPersModel[pm]->vignette.bad_error = true; filtered++; } - if (aPersModel[pm]->hasModeData (1) && aPersModel[pm]->base.mean_error > maxAvgDevFac * errBase) { + if (aPersModel[pm]->hasModeData(1) && aPersModel[pm]->base.mean_error > maxAvgDevFac * errBase) { aPersModel[pm]->base.bad_error = true; filtered++; } - if (aPersModel[pm]->hasModeData (2) && + if (aPersModel[pm]->hasModeData(2) && (aPersModel[pm]->chromRG.mean_error > maxAvgDevFac * errChrom || aPersModel[pm]->chromG.mean_error > maxAvgDevFac * errChrom || aPersModel[pm]->chromBG.mean_error > maxAvgDevFac * errChrom)) { aPersModel[pm]->chromRG.bad_error = aPersModel[pm]->chromG.bad_error = aPersModel[pm]->chromBG.bad_error = true; @@ -444,22 +450,23 @@ int LCPProfile::filterBadFrames (double maxAvgDevFac, int minFramesLeft) return filtered; } + // mode: 0=vignette, 1=distortion, 2=CA -void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const +void LCPProfile::calcParams(int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const { - float euler = exp (1.0); + float euler = exp(1.0); // find the frames with the least distance, focal length wise LCPPersModel *pLow = nullptr, *pHigh = nullptr; - float focalLengthLog = log (focalLength); //, apertureLog=aperture>0 ? log(aperture) : 0; - float focusDistLog = focusDist > 0 ? log (focusDist) + euler : 0; + float focalLengthLog = log(focalLength); //, apertureLog=aperture>0 ? log(aperture) : 0; + float focusDistLog = focusDist > 0 ? log(focusDist) + euler : 0; // Pass 1: determining best focal length, if possible different focusDistances (for the focDist is not given case) for (int pm = 0; pm < persModelCount; pm++) { float f = aPersModel[pm]->focLen; - if (aPersModel[pm]->hasModeData (mode)) { + if (aPersModel[pm]->hasModeData(mode)) { if (f <= focalLength && (pLow == nullptr || f > pLow->focLen || (focusDist == 0 && f == pLow->focLen && pLow->focDist > aPersModel[pm]->focDist))) { pLow = aPersModel[pm]; } @@ -483,10 +490,10 @@ void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float for (int pm = 0; pm < persModelCount; pm++) { float aper = aPersModel[pm]->aperture; // float aperLog=log(aper); float focDist = aPersModel[pm]->focDist; - float focDistLog = log (focDist) + euler; + float focDistLog = log(focDist) + euler; double meanErr; - if (aPersModel[pm]->hasModeData (mode)) { + if (aPersModel[pm]->hasModeData(mode)) { if (mode == 0) { meanErr = aPersModel[pm]->vignette.mean_error; @@ -495,14 +502,14 @@ void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float if (aPersModel[pm]->focLen == bestFocLenLow && ( (aper == aperture && pLow->vignette.mean_error > meanErr) || (aper >= aperture && aper < pLow->aperture && pLow->aperture > aperture) - || (aper <= aperture && (pLow->aperture > aperture || fabs (aperture - aper) < fabs (aperture - pLow->aperture))))) { + || (aper <= aperture && (pLow->aperture > aperture || fabs(aperture - aper) < fabs(aperture - pLow->aperture))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( (aper == aperture && pHigh->vignette.mean_error > meanErr) || (aper <= aperture && aper > pHigh->aperture && pHigh->aperture < aperture) - || (aper >= aperture && (pHigh->aperture < aperture || fabs (aperture - aper) < fabs (aperture - pHigh->aperture))))) { + || (aper >= aperture && (pHigh->aperture < aperture || fabs(aperture - aper) < fabs(aperture - pHigh->aperture))))) { pHigh = aPersModel[pm]; } } else { @@ -513,14 +520,14 @@ void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float if (aPersModel[pm]->focLen == bestFocLenLow && ( (focDist == focusDist && (mode == 1 ? pLow->base.mean_error : pLow->chromG.mean_error) > meanErr) || (focDist >= focusDist && focDist < pLow->focDist && pLow->focDist > focusDist) - || (focDist <= focusDist && (pLow->focDist > focusDist || fabs (focusDistLog - focDistLog) < fabs (focusDistLog - (log (pLow->focDist) + euler)))))) { + || (focDist <= focusDist && (pLow->focDist > focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pLow->focDist) + euler)))))) { pLow = aPersModel[pm]; } if (aPersModel[pm]->focLen == bestFocLenHigh && ( (focDist == focusDist && (mode == 1 ? pHigh->base.mean_error : pHigh->chromG.mean_error) > meanErr) || (focDist <= focusDist && focDist > pHigh->focDist && pHigh->focDist < focusDist) - || (focDist >= focusDist && (pHigh->focDist < focusDist || fabs (focusDistLog - focDistLog) < fabs (focusDistLog - (log (pHigh->focDist) + euler)))))) { + || (focDist >= focusDist && (pHigh->focDist < focusDist || fabs(focusDistLog - focDistLog) < fabs(focusDistLog - (log(pHigh->focDist) + euler)))))) { pHigh = aPersModel[pm]; } } else { @@ -545,7 +552,7 @@ void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float // There is as foclen range, take that as basis if (pLow->focLen < pHigh->focLen) { - facLow = (log (pHigh->focLen) - focalLengthLog) / (log (pHigh->focLen) - log (pLow->focLen)); + facLow = (log(pHigh->focLen) - focalLengthLog) / (log(pHigh->focLen) - log(pLow->focLen)); } else { focLenOnSpot = pLow->focLen == pHigh->focLen && pLow->focLen == focalLength; } @@ -557,45 +564,45 @@ void LCPProfile::calcParams (int mode, float focalLength, float focusDist, float facLow = focLenOnSpot ? facAperLow : (0.5 * facLow + 0.5 * facAperLow); } else if (mode != 0 && focusDist > 0 && pLow->focDist < focusDist && pHigh->focDist > focusDist) { // focus distance for all else (if focus distance is given) - float facDistLow = (log (pHigh->focDist) + euler - focusDistLog) / (log (pHigh->focDist) - log (pLow->focDist)); + float facDistLow = (log(pHigh->focDist) + euler - focusDistLog) / (log(pHigh->focDist) - log(pLow->focDist)); facLow = focLenOnSpot ? facDistLow : (0.8 * facLow + 0.2 * facDistLow); } switch (mode) { - case 0: // vignette - pCorr1->merge (pLow->vignette, pHigh->vignette, facLow); - break; + case 0: // vignette + pCorr1->merge(pLow->vignette, pHigh->vignette, facLow); + break; - case 1: // distortion - pCorr1->merge (pLow->base, pHigh->base, facLow); - break; + case 1: // distortion + pCorr1->merge(pLow->base, pHigh->base, facLow); + break; - case 2: // CA - pCorr1->merge (pLow->chromRG, pHigh->chromRG, facLow); - pCorr2->merge (pLow->chromG, pHigh->chromG, facLow); - pCorr3->merge (pLow->chromBG, pHigh->chromBG, facLow); - break; + case 2: // CA + pCorr1->merge(pLow->chromRG, pHigh->chromRG, facLow); + pCorr2->merge(pLow->chromG, pHigh->chromG, facLow); + pCorr3->merge(pLow->chromBG, pHigh->chromBG, facLow); + break; } //printf("LCP mode=%i, dist: %g found frames: Fno %g-%g; FocLen %g-%g; Dist %g-%g with weight %g\n", mode, focusDist, pLow->aperture, pHigh->aperture, pLow->focLen, pHigh->focLen, pLow->focDist, pHigh->focDist, facLow); } else { - printf ("Error: LCP file contained no %s parameters\n", mode == 0 ? "vignette" : mode == 1 ? "distortion" : "CA" ); + printf("Error: LCP file contained no %s parameters\n", mode == 0 ? "vignette" : mode == 1 ? "distortion" : "CA" ); } } void LCPProfile::print() const { - printf ("=== Profile %s\n", profileName.c_str()); - printf ("Frames: %i, RAW: %i; Fisheye: %i; Sensorformat: %f\n", persModelCount, isRaw, isFisheye, sensorFormatFactor); + printf("=== Profile %s\n", profileName.c_str()); + printf("Frames: %i, RAW: %i; Fisheye: %i; Sensorformat: %f\n", persModelCount, isRaw, isFisheye, sensorFormatFactor); for (int pm = 0; pm < persModelCount; pm++) { aPersModel[pm]->print(); } } -void XMLCALL LCPProfile::XmlStartHandler (void *pLCPProfile, const char *el, const char **attr) +void XMLCALL LCPProfile::XmlStartHandler(void *pLCPProfile, const char *el, const char **attr) { - LCPProfile *pProf = static_cast (pLCPProfile); + LCPProfile *pProf = static_cast(pLCPProfile); bool parseAttr = false; if (*pProf->inInvalidTag) { @@ -603,29 +610,29 @@ void XMLCALL LCPProfile::XmlStartHandler (void *pLCPProfile, const char *el, con } // clean up tagname - const char* src = strrchr (el, ':'); + const char* src = strrchr(el, ':'); if (src == nullptr) { - src = const_cast (el); + src = const_cast(el); } else { src++; } - strcpy (pProf->lastTag, src); + strcpy(pProf->lastTag, src); - if (!strcmp ("VignetteModelPiecewiseParam", src)) { - strcpy (pProf->inInvalidTag, src); + if (!strcmp("VignetteModelPiecewiseParam", src)) { + strcpy(pProf->inInvalidTag, src); } - if (!strcmp ("CameraProfiles", src)) { + if (!strcmp("CameraProfiles", src)) { pProf->inCamProfiles = true; } - if (!strcmp ("AlternateLensIDs", src)) { + if (!strcmp("AlternateLensIDs", src)) { pProf->inAlternateLensID = true; } - if (!strcmp ("AlternateLensNames", src)) { + if (!strcmp("AlternateLensNames", src)) { pProf->inAlternateLensNames = true; } @@ -633,37 +640,37 @@ void XMLCALL LCPProfile::XmlStartHandler (void *pLCPProfile, const char *el, con return; } - if (!strcmp ("li", src)) { + if (!strcmp("li", src)) { pProf->pCurPersModel = new LCPPersModel(); pProf->pCurCommon = &pProf->pCurPersModel->base; // iterated to next tags within persModel return; } - if (!strcmp ("PerspectiveModel", src)) { + if (!strcmp("PerspectiveModel", src)) { pProf->firstLIDone = true; pProf->inPerspect = true; return; - } else if (!strcmp ("FisheyeModel", src)) { + } else if (!strcmp("FisheyeModel", src)) { pProf->firstLIDone = true; pProf->inPerspect = true; pProf->isFisheye = true; // just misses third param, and different path, rest is the same return; - } else if (!strcmp ("Description", src)) { + } else if (!strcmp("Description", src)) { parseAttr = true; } // Move pointer to general section if (pProf->inPerspect) { - if (!strcmp ("ChromaticRedGreenModel", src)) { + if (!strcmp("ChromaticRedGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromRG; parseAttr = true; - } else if (!strcmp ("ChromaticGreenModel", src)) { + } else if (!strcmp("ChromaticGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromG; parseAttr = true; - } else if (!strcmp ("ChromaticBlueGreenModel", src)) { + } else if (!strcmp("ChromaticBlueGreenModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->chromBG; parseAttr = true; - } else if (!strcmp ("VignetteModel", src)) { + } else if (!strcmp("VignetteModel", src)) { pProf->pCurCommon = &pProf->pCurPersModel->vignette; parseAttr = true; } @@ -673,23 +680,23 @@ void XMLCALL LCPProfile::XmlStartHandler (void *pLCPProfile, const char *el, con // simulate tags by feeding them in if (parseAttr && attr != nullptr) { for (int i = 0; attr[i]; i += 2) { - const char* nameStart = strrchr (attr[i], ':'); + const char* nameStart = strrchr(attr[i], ':'); if (nameStart == nullptr) { - nameStart = const_cast (attr[i]); + nameStart = const_cast(attr[i]); } else { nameStart++; } - strcpy (pProf->lastTag, nameStart); - XmlTextHandler (pLCPProfile, attr[i + 1], strlen (attr[i + 1])); + strcpy(pProf->lastTag, nameStart); + XmlTextHandler(pLCPProfile, attr[i + 1], strlen(attr[i + 1])); } } } -void XMLCALL LCPProfile::XmlTextHandler (void *pLCPProfile, const XML_Char *s, int len) +void XMLCALL LCPProfile::XmlTextHandler(void *pLCPProfile, const XML_Char *s, int len) { - LCPProfile *pProf = static_cast (pLCPProfile); + LCPProfile *pProf = static_cast(pLCPProfile); if (!pProf->inCamProfiles || pProf->inAlternateLensID || pProf->inAlternateLensNames || *pProf->inInvalidTag) { return; @@ -700,7 +707,7 @@ void XMLCALL LCPProfile::XmlTextHandler (void *pLCPProfile, const XML_Char *s, i int i = 0; while (i < len && onlyWhiteSpace) { - onlyWhiteSpace = isspace (s[i]); + onlyWhiteSpace = isspace(s[i]); i++; } @@ -710,32 +717,32 @@ void XMLCALL LCPProfile::XmlTextHandler (void *pLCPProfile, const XML_Char *s, i // convert to null terminated char raw[len + 1]; - memcpy (raw, s, len); + memcpy(raw, s, len); raw[len] = 0; char* tag = pProf->lastTag; // Common data section if (!pProf->firstLIDone) { // Generic tags are the same for all - if (!strcmp ("ProfileName", tag)) { - pProf->profileName = Glib::ustring (raw); - } else if (!strcmp ("Model", tag)) { - pProf->camera = Glib::ustring (raw); - } else if (!strcmp ("Lens", tag)) { - pProf->lens = Glib::ustring (raw); - } else if (!strcmp ("CameraPrettyName", tag)) { - pProf->cameraPrettyName = Glib::ustring (raw); - } else if (!strcmp ("LensPrettyName", tag)) { - pProf->lensPrettyName = Glib::ustring (raw); - } else if (!strcmp ("CameraRawProfile", tag)) { - pProf->isRaw = !strcmp ("True", raw); + if (!strcmp("ProfileName", tag)) { + pProf->profileName = Glib::ustring(raw); + } else if (!strcmp("Model", tag)) { + pProf->camera = Glib::ustring(raw); + } else if (!strcmp("Lens", tag)) { + pProf->lens = Glib::ustring(raw); + } else if (!strcmp("CameraPrettyName", tag)) { + pProf->cameraPrettyName = Glib::ustring(raw); + } else if (!strcmp("LensPrettyName", tag)) { + pProf->lensPrettyName = Glib::ustring(raw); + } else if (!strcmp("CameraRawProfile", tag)) { + pProf->isRaw = !strcmp("True", raw); } } // --- Now all floating points. Must replace local dot characters // WARNING: called by different threads, that may run on different local settings, // so don't use system params - if (atof ("1,2345") == 1.2345) { + if (atof("1,2345") == 1.2345) { char* p = raw; while (*p) { @@ -748,69 +755,69 @@ void XMLCALL LCPProfile::XmlTextHandler (void *pLCPProfile, const XML_Char *s, i } if (!pProf->firstLIDone) { - if (!strcmp ("SensorFormatFactor", tag)) { - pProf->sensorFormatFactor = atof (raw); + if (!strcmp("SensorFormatFactor", tag)) { + pProf->sensorFormatFactor = atof(raw); } } // Perspective model base data - if (!strcmp ("FocalLength", tag)) { - pProf->pCurPersModel->focLen = atof (raw); - } else if (!strcmp ("FocusDistance", tag)) { - double focDist = atof (raw); + if (!strcmp("FocalLength", tag)) { + pProf->pCurPersModel->focLen = atof(raw); + } else if (!strcmp("FocusDistance", tag)) { + double focDist = atof(raw); pProf->pCurPersModel->focDist = focDist < 10000 ? focDist : 10000; - } else if (!strcmp ("ApertureValue", tag)) { - pProf->pCurPersModel->aperture = atof (raw); + } else if (!strcmp("ApertureValue", tag)) { + pProf->pCurPersModel->aperture = atof(raw); } // Section depended - if (!strcmp ("FocalLengthX", tag)) { - pProf->pCurCommon->foc_len_x = atof (raw); - } else if (!strcmp ("FocalLengthY", tag)) { - pProf->pCurCommon->foc_len_y = atof (raw); - } else if (!strcmp ("ImageXCenter", tag)) { - pProf->pCurCommon->img_center_x = atof (raw); - } else if (!strcmp ("ImageYCenter", tag)) { - pProf->pCurCommon->img_center_y = atof (raw); - } else if (!strcmp ("ScaleFactor", tag)) { - pProf->pCurCommon->scale_factor = atof (raw); - } else if (!strcmp ("ResidualMeanError", tag)) { - pProf->pCurCommon->mean_error = atof (raw); - } else if (!strcmp ("RadialDistortParam1", tag) || !strcmp ("VignetteModelParam1", tag)) { - pProf->pCurCommon->param[0] = atof (raw); - } else if (!strcmp ("RadialDistortParam2", tag) || !strcmp ("VignetteModelParam2", tag)) { - pProf->pCurCommon->param[1] = atof (raw); - } else if (!strcmp ("RadialDistortParam3", tag) || !strcmp ("VignetteModelParam3", tag)) { - pProf->pCurCommon->param[2] = atof (raw); - } else if (!strcmp ("RadialDistortParam4", tag) || !strcmp ("TangentialDistortParam1", tag)) { - pProf->pCurCommon->param[3] = atof (raw); - } else if (!strcmp ("RadialDistortParam5", tag) || !strcmp ("TangentialDistortParam2", tag)) { - pProf->pCurCommon->param[4] = atof (raw); + if (!strcmp("FocalLengthX", tag)) { + pProf->pCurCommon->foc_len_x = atof(raw); + } else if (!strcmp("FocalLengthY", tag)) { + pProf->pCurCommon->foc_len_y = atof(raw); + } else if (!strcmp("ImageXCenter", tag)) { + pProf->pCurCommon->img_center_x = atof(raw); + } else if (!strcmp("ImageYCenter", tag)) { + pProf->pCurCommon->img_center_y = atof(raw); + } else if (!strcmp("ScaleFactor", tag)) { + pProf->pCurCommon->scale_factor = atof(raw); + } else if (!strcmp("ResidualMeanError", tag)) { + pProf->pCurCommon->mean_error = atof(raw); + } else if (!strcmp("RadialDistortParam1", tag) || !strcmp("VignetteModelParam1", tag)) { + pProf->pCurCommon->param[0] = atof(raw); + } else if (!strcmp("RadialDistortParam2", tag) || !strcmp("VignetteModelParam2", tag)) { + pProf->pCurCommon->param[1] = atof(raw); + } else if (!strcmp("RadialDistortParam3", tag) || !strcmp("VignetteModelParam3", tag)) { + pProf->pCurCommon->param[2] = atof(raw); + } else if (!strcmp("RadialDistortParam4", tag) || !strcmp("TangentialDistortParam1", tag)) { + pProf->pCurCommon->param[3] = atof(raw); + } else if (!strcmp("RadialDistortParam5", tag) || !strcmp("TangentialDistortParam2", tag)) { + pProf->pCurCommon->param[4] = atof(raw); } } -void XMLCALL LCPProfile::XmlEndHandler (void *pLCPProfile, const char *el) +void XMLCALL LCPProfile::XmlEndHandler(void *pLCPProfile, const char *el) { - LCPProfile *pProf = static_cast (pLCPProfile); + LCPProfile *pProf = static_cast(pLCPProfile); // We ignore everything in dirty tag till it's gone if (*pProf->inInvalidTag) { - if (strstr (el, pProf->inInvalidTag)) { + if (strstr(el, pProf->inInvalidTag)) { *pProf->inInvalidTag = 0; } return; } - if (strstr (el, ":CameraProfiles")) { + if (strstr(el, ":CameraProfiles")) { pProf->inCamProfiles = false; } - if (strstr (el, ":AlternateLensIDs")) { + if (strstr(el, ":AlternateLensIDs")) { pProf->inAlternateLensID = false; } - if (strstr (el, ":AlternateLensNames")) { + if (strstr(el, ":AlternateLensNames")) { pProf->inAlternateLensNames = false; } @@ -818,9 +825,9 @@ void XMLCALL LCPProfile::XmlEndHandler (void *pLCPProfile, const char *el) return; } - if (strstr (el, ":PerspectiveModel") || strstr (el, ":FisheyeModel")) { + if (strstr(el, ":PerspectiveModel") || strstr(el, ":FisheyeModel")) { pProf->inPerspect = false; - } else if (strstr (el, ":li")) { + } else if (strstr(el, ":li")) { pProf->aPersModel[pProf->persModelCount] = pProf->pCurPersModel; pProf->pCurPersModel = nullptr; pProf->persModelCount++; @@ -836,11 +843,11 @@ LCPStore* LCPStore::getInstance() LCPProfile* LCPStore::getProfile (Glib::ustring filename) { - if (filename.length() == 0 || !isValidLCPFileName (filename)) { + if (filename.length() == 0 || !isValidLCPFileName(filename)) { return nullptr; } - MyMutex::MyLock lock (mtx); + MyMutex::MyLock lock(mtx); std::map::iterator r = profileCache.find (filename); @@ -849,12 +856,12 @@ LCPProfile* LCPStore::getProfile (Glib::ustring filename) } // Add profile (if exists) - profileCache[filename] = new LCPProfile (filename); + profileCache[filename] = new LCPProfile(filename); //profileCache[filename]->print(); return profileCache[filename]; } -bool LCPStore::isValidLCPFileName (Glib::ustring filename) const +bool LCPStore::isValidLCPFileName(Glib::ustring filename) const { if (!Glib::file_test (filename, Glib::FILE_TEST_EXISTS) || Glib::file_test (filename, Glib::FILE_TEST_IS_DIR)) { return false; @@ -872,10 +879,10 @@ Glib::ustring LCPStore::getDefaultCommonDirectory() const WCHAR pathW[MAX_PATH] = {0}; char pathA[MAX_PATH]; - if (SHGetSpecialFolderPathW (NULL, pathW, CSIDL_COMMON_APPDATA, false)) { + if (SHGetSpecialFolderPathW(NULL, pathW, CSIDL_COMMON_APPDATA, false)) { char pathA[MAX_PATH]; - WideCharToMultiByte (CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0); - Glib::ustring fullDir = Glib::ustring (pathA) + Glib::ustring ("\\Adobe\\CameraRaw\\LensProfiles\\1.0"); + WideCharToMultiByte(CP_UTF8, 0, pathW, -1, pathA, MAX_PATH, 0, 0); + Glib::ustring fullDir = Glib::ustring(pathA) + Glib::ustring("\\Adobe\\CameraRaw\\LensProfiles\\1.0"); if (Glib::file_test (fullDir, Glib::FILE_TEST_IS_DIR)) { dir = fullDir; diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 9488f74d7..92c984921 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -39,8 +39,8 @@ public: LCPModelCommon(); bool empty() const; // is it empty void print() const; // printf all values - void merge (const LCPModelCommon& a, const LCPModelCommon& b, float facA); - void prepareParams (int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY); + void merge(const LCPModelCommon& a, const LCPModelCommon& b, float facA); + void prepareParams(int fullWidth, int fullHeight, float focalLength, float focalLength35mm, float sensorFormatFactor, bool swapXY, bool mirrorX, bool mirrorY); //private: using Param = std::array; @@ -75,7 +75,7 @@ public: LCPModelCommon vignette; // vignette (may be empty) LCPPersModel(); - bool hasModeData (int mode) const; + bool hasModeData(int mode) const; void print() const; }; @@ -88,11 +88,11 @@ class LCPProfile LCPPersModel* pCurPersModel; LCPModelCommon* pCurCommon; - static void XMLCALL XmlStartHandler (void *pLCPProfile, const char *el, const char **attr); + static void XMLCALL XmlStartHandler(void *pLCPProfile, const char *el, const char **attr); static void XMLCALL XmlTextHandler (void *pLCPProfile, const XML_Char *s, int len); static void XMLCALL XmlEndHandler (void *pLCPProfile, const char *el); - int filterBadFrames (double maxAvgDevFac, int minFramesLeft); + int filterBadFrames(double maxAvgDevFac, int minFramesLeft); public: // Common data @@ -105,9 +105,9 @@ public: static const int MaxPersModelCount = 3000; LCPPersModel* aPersModel[MaxPersModelCount]; // Do NOT use std::list or something, it's buggy in GCC! - explicit LCPProfile (const Glib::ustring &fname); + explicit LCPProfile(const Glib::ustring &fname); - void calcParams (int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const; // Interpolates between the persModels frames + void calcParams(int mode, float focalLength, float focusDist, float aperture, LCPModelCommon *pCorr1, LCPModelCommon *pCorr2, LCPModelCommon *pCorr3) const; // Interpolates between the persModels frames void print() const; }; @@ -121,8 +121,8 @@ class LCPStore public: Glib::ustring getDefaultCommonDirectory() const; - bool isValidLCPFileName (Glib::ustring filename) const; - LCPProfile* getProfile (Glib::ustring filename); + bool isValidLCPFileName(Glib::ustring filename) const; + LCPProfile* getProfile(Glib::ustring filename); static LCPStore* getInstance(); }; @@ -143,13 +143,14 @@ public: bool enableCA; // is the mapper capable if CA correction? // precalculates the mapper. - LCPMapper (LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, int fullWidth, int fullHeight, - const CoarseTransformParams& coarse, int rawRotationDeg); + LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, int fullWidth, int fullHeight, + const CoarseTransformParams& coarse, int rawRotationDeg); - void correctDistortion (double& x, double& y) const; // MUST be the first stage - void correctCA (double& x, double& y, int channel) const; - void processVignetteLine (int width, int y, float *line) const; - void processVignetteLine3Channels (int width, int y, float *line) const; + void correctDistortion(double& x, double& y) const; // MUST be the first stage + void correctCA(double& x, double& y, int channel) const; + float calcVignetteFac (int x, int y) const; // MUST be in RAW + void processVignetteLine(int width, int y, float *line) const; + void processVignetteLine3Channels(int width, int y, float *line) const; }; } From 7d9e5765ba4f181c3235205d81af1a340d1e94e9 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Wed, 22 Feb 2017 14:50:10 +0100 Subject: [PATCH 6/6] Removed unused function and stopwatch --- rtengine/lcp.cc | 11 ----------- rtengine/lcp.h | 5 ++--- rtengine/rawimagesource.cc | 1 - 3 files changed, 2 insertions(+), 15 deletions(-) diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index ff53ad30e..310361a72 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -265,17 +265,6 @@ void LCPMapper::correctCA(double& x, double& y, int channel) const } } -float LCPMapper::calcVignetteFac(int x, int y) const -{ - // No need for swapXY, since vignette is in RAW and always before rotation - float xd = ((float)x - mc.x0) * mc.rfx, yd = ((float)y - mc.y0) * mc.rfy; - - const LCPModelCommon::VignParam vignParam = mc.vign_param; - float rsqr = xd * xd + yd * yd; - - return rsqr * (vignParam[0] + rsqr * ((vignParam[1]) - (vignParam[2]) * rsqr + (vignParam[3]) * rsqr * rsqr)); -} - SSEFUNCTION void LCPMapper::processVignetteLine(int width, int y, float *line) const { // No need for swapXY, since vignette is in RAW and always before rotation diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 92c984921..fa8cf226d 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -146,9 +146,8 @@ public: LCPMapper(LCPProfile* pProf, float focalLength, float focalLength35mm, float focusDist, float aperture, bool vignette, bool useCADistP, int fullWidth, int fullHeight, const CoarseTransformParams& coarse, int rawRotationDeg); - void correctDistortion(double& x, double& y) const; // MUST be the first stage - void correctCA(double& x, double& y, int channel) const; - float calcVignetteFac (int x, int y) const; // MUST be in RAW + void correctDistortion(double& x, double& y) const; // MUST be the first stage + void correctCA(double& x, double& y, int channel) const; void processVignetteLine(int width, int y, float *line) const; void processVignetteLine3Channels(int width, int y, float *line) const; }; diff --git a/rtengine/rawimagesource.cc b/rtengine/rawimagesource.cc index 995e3d9bd..9678fcaf5 100644 --- a/rtengine/rawimagesource.cc +++ b/rtengine/rawimagesource.cc @@ -1767,7 +1767,6 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le LCPProfile *pLCPProf = lcpStore->getProfile(lensProf.lcpFile); if (pLCPProf) { // don't check focal length to allow distortion correction for lenses without chip, also pass dummy focal length 1 in case of 0 - StopWatch Stop1("lcp vignette correction"); LCPMapper map(pLCPProf, max(idata->getFocalLen(), 1.0), idata->getFocalLen35mm(), idata->getFocusDist(), idata->getFNumber(), true, false, W, H, coarse, -1); if (ri->getSensorType() == ST_BAYER || ri->getSensorType() == ST_FUJI_XTRANS || ri->get_colors() == 1) {