From 512517327ff93d32b0bcce39d8a101ce7332eaac Mon Sep 17 00:00:00 2001 From: Lawrence Date: Thu, 2 Jan 2020 17:10:41 -0800 Subject: [PATCH 1/3] Move profiled distortion correction in pipeline Lensfun/LCP distortion correction was applied before automatic scaling and perspective correction in the inverse transformation. This means it was applied after scaling and perspective in the pipeline. It should actually come first. --- rtengine/iptransform.cc | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index af513536e..bc490ce82 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -253,15 +253,8 @@ bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, for (size_t i = 0; i < src.size(); i++) { double x_d = src[i].x, y_d = src[i].y; - if (pLCPMap && params->lensProf.useDist) { - pLCPMap->correctDistortion(x_d, y_d, 0, 0, ascale); - } else { - x_d *= ascale; - y_d *= ascale; - } - - x_d += ascale * (0 - w2); // centering x coord & scale - y_d += ascale * (0 - h2); // centering y coord & scale + y_d = ascale * (y_d - h2); // centering x coord & scale + x_d = ascale * (x_d - w2); // centering x coord & scale if (needsPerspective()) { // horizontal perspective transformation @@ -273,6 +266,10 @@ bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, y_d *= maxRadius * vpcospt / (maxRadius - y_d * vptanpt); } + if (pLCPMap && params->lensProf.useDist) { + pLCPMap->correctDistortion(x_d, y_d, 0, 0, 1); + } + // rotate double Dx = x_d * cost - y_d * sint; double Dy = x_d * sint + y_d * cost; @@ -935,15 +932,8 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I double x_d = x; double y_d = y; - if (enableLCPDist) { - pLCPMap->correctDistortion(x_d, y_d, cx, cy, ascale); // must be first transform - } else { - x_d *= ascale; - y_d *= ascale; - } - - x_d += ascale * centerFactorx; // centering x coord & scale - y_d += ascale * centerFactory; // centering y coord & scale + x_d = ascale * (x_d + centerFactorx); // centering x coord & scale + y_d = ascale * (y_d + centerFactory); // centering y coord & scale if (enablePerspective) { // horizontal perspective transformation @@ -955,6 +945,10 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I y_d *= maxRadius * vpcospt / (maxRadius - y_d * vptanpt); } + if (enableLCPDist) { + pLCPMap->correctDistortion(x_d, y_d, w2, h2, 1); // must be first transform + } + // rotate const double Dxc = x_d * cost - y_d * sint; const double Dyc = x_d * sint + y_d * cost; From b266cb7ca3380257ec34ea8fe39772cb5a36f61a Mon Sep 17 00:00:00 2001 From: Lawrence Date: Thu, 2 Jan 2020 18:33:35 -0800 Subject: [PATCH 2/3] Remove scale parameter from distortion correction Scale is no longer useful in LensCorrection::correctDistortion. Remove the scale parameter from the functions. --- rtengine/iptransform.cc | 4 ++-- rtengine/lcp.cc | 22 ++++++++++------------ rtengine/lcp.h | 4 ++-- rtengine/rtlensfun.cc | 4 +--- rtengine/rtlensfun.h | 2 +- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index bc490ce82..668756b71 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -267,7 +267,7 @@ bool ImProcFunctions::transCoord (int W, int H, const std::vector &src, } if (pLCPMap && params->lensProf.useDist) { - pLCPMap->correctDistortion(x_d, y_d, 0, 0, 1); + pLCPMap->correctDistortion(x_d, y_d, 0, 0); } // rotate @@ -946,7 +946,7 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I } if (enableLCPDist) { - pLCPMap->correctDistortion(x_d, y_d, w2, h2, 1); // must be first transform + pLCPMap->correctDistortion(x_d, y_d, w2, h2); // must be first transform } // rotate diff --git a/rtengine/lcp.cc b/rtengine/lcp.cc index c80a126f5..a4dea2055 100644 --- a/rtengine/lcp.cc +++ b/rtengine/lcp.cc @@ -1030,16 +1030,16 @@ bool rtengine::LCPMapper::isCACorrectionAvailable() const return enableCA; } -void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy, double scale) const +void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy) const { x += cx; y += cy; if (isFisheye) { - const double u = x * scale; - const double v = y * scale; - const double u0 = mc.x0 * scale; - const double v0 = mc.y0 * scale; + const double u = x; + const double v = y; + const double u0 = mc.x0; + const double v0 = mc.y0; const double du = (u - u0); const double dv = (v - v0); const double fx = mc.fx; @@ -1047,7 +1047,7 @@ void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy const double k1 = mc.param[0]; const double k2 = mc.param[1]; const double r = sqrt(du * du + dv * dv); - const double f = sqrt(fx*fy / (scale * scale)); + const double f = sqrt(fx*fy); const double th = atan2(r, f); const double th2 = th * th; const double cfact = (((k2 * th2 + k1) * th2 + 1) * th) / r; @@ -1057,10 +1057,8 @@ void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy x = ud; y = vd; } else { - x *= scale; - y *= scale; - const double x0 = mc.x0 * scale; - const double y0 = mc.y0 * scale; + const double x0 = mc.x0; + const double y0 = mc.y0; const double xd = (x - x0) / mc.fx, yd = (y - y0) / mc.fy; const LCPModelCommon::Param aDist = mc.param; @@ -1077,8 +1075,8 @@ void rtengine::LCPMapper::correctDistortion(double &x, double &y, int cx, int cy y = ynew * mc.fy + y0; } - x -= cx * scale; - y -= cy * scale; + x -= cx; + y -= cy; } void rtengine::LCPMapper::correctCA(double& x, double& y, int cx, int cy, int channel) const diff --git a/rtengine/lcp.h b/rtengine/lcp.h index 2e36fe113..e6ecbfc97 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -168,7 +168,7 @@ private: class LensCorrection { public: virtual ~LensCorrection() {} - virtual void correctDistortion(double &x, double &y, int cx, int cy, double scale) const = 0; + virtual void correctDistortion(double &x, double &y, int cx, int cy) const = 0; virtual bool isCACorrectionAvailable() const = 0; virtual void correctCA(double &x, double &y, int cx, int cy, int channel) const = 0; virtual void processVignette(int width, int height, float** rawData) const = 0; @@ -196,7 +196,7 @@ public: ); - void correctDistortion(double &x, double &y, int cx, int cy, double scale) const override; // MUST be the first stage + void correctDistortion(double &x, double &y, int cx, int cy) const override; // MUST be the first stage bool isCACorrectionAvailable() const override; void correctCA(double& x, double& y, int cx, int cy, int channel) const override; void processVignette(int width, int height, float** rawData) const override; diff --git a/rtengine/rtlensfun.cc b/rtengine/rtlensfun.cc index 7ffb9ad33..363819cf7 100644 --- a/rtengine/rtlensfun.cc +++ b/rtengine/rtlensfun.cc @@ -46,7 +46,7 @@ LFModifier::operator bool() const } -void LFModifier::correctDistortion(double &x, double &y, int cx, int cy, double scale) const +void LFModifier::correctDistortion(double &x, double &y, int cx, int cy) const { if (!data_) { return; @@ -67,8 +67,6 @@ void LFModifier::correctDistortion(double &x, double &y, int cx, int cy, double x -= cx; y -= cy; } - x *= scale; - y *= scale; } diff --git a/rtengine/rtlensfun.h b/rtengine/rtlensfun.h index 2f3e4677d..51212c9b9 100644 --- a/rtengine/rtlensfun.h +++ b/rtengine/rtlensfun.h @@ -53,7 +53,7 @@ public: explicit operator bool() const; - void correctDistortion(double &x, double &y, int cx, int cy, double scale) const override; + void correctDistortion(double &x, double &y, int cx, int cy) const override; bool isCACorrectionAvailable() const override; void correctCA(double &x, double &y, int cx, int cy, int channel) const override; void processVignette(int width, int height, float** rawData) const override; From 72de3f21c7ccff53c5a91f2953c64bc4645f7e2d Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Thu, 25 Jun 2020 10:39:21 -0700 Subject: [PATCH 3/3] Remove misleading comments --- rtengine/iptransform.cc | 2 +- rtengine/lcp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rtengine/iptransform.cc b/rtengine/iptransform.cc index 29f2f25eb..f8a15cbfd 100644 --- a/rtengine/iptransform.cc +++ b/rtengine/iptransform.cc @@ -1089,7 +1089,7 @@ void ImProcFunctions::transformGeneral(bool highQuality, Imagefloat *original, I } if (enableLCPDist) { - pLCPMap->correctDistortion(x_d, y_d, w2, h2); // must be first transform + pLCPMap->correctDistortion(x_d, y_d, w2, h2); } // rotate diff --git a/rtengine/lcp.h b/rtengine/lcp.h index de9ff045f..b59cc84c6 100644 --- a/rtengine/lcp.h +++ b/rtengine/lcp.h @@ -194,7 +194,7 @@ public: ); - void correctDistortion(double &x, double &y, int cx, int cy) const override; // MUST be the first stage + void correctDistortion(double &x, double &y, int cx, int cy) const override; bool isCACorrectionAvailable() const override; void correctCA(double& x, double& y, int cx, int cy, int channel) const override; void processVignette(int width, int height, float** rawData) const override;